C# 在 .NET 1.1 中将 StringBuilder 的内容写入文本文件的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15443739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the simplest way to write the contents of a StringBuilder to a text file in .NET 1.1?
提问by B. Clay Shannon
I have to use StringBuilder instead of a List of strings because of being stuck with .NET 1.1 for this project.
我必须使用 StringBuilder 而不是字符串列表,因为该项目一直使用 .NET 1.1。
I want to write a series of debug messages I've written to a file to study at my leisure, as there is too much to see on the screen (the MessageBox doesn't have scrollbars). Some of the easy ways to write a file don't seem to be available in .NET 1.1. I also don't have access to Environment.Newline
to cleanly separate the lines I append (AppendLine
is not available in this archaic version of StringBuilder, either).
我想编写一系列已写入文件的调试消息,以便在闲暇时研究,因为屏幕上有太多可看的东西(MessageBox 没有滚动条)。.NET 1.1 中似乎没有一些编写文件的简单方法。我也无法Environment.Newline
干净地分隔我附加的行(AppendLine
在这个古老的 StringBuilder 版本中也不可用)。
What is the easiest way in .NET 1.1 (C#) to write out the contents of the StringBuilder to a file? There is no "C" drive on the handheld device, so I reckon I will have to write it to "\hereIAm.txt" or something.
.NET 1.1 (C#) 中将 StringBuilder 的内容写入文件的最简单方法是什么?手持设备上没有“C”驱动器,因此我认为我必须将其写入“\hereIAm.txt”或其他内容。
采纳答案by Darren
You still have access to StreamWriter
:
您仍然可以访问StreamWriter
:
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"\hereIam.txt"))
{
file.WriteLine(sb.ToString()); // "sb" is the StringBuilder
}
From the MSDN documentation: Writing to a Text File (Visual C#).
来自 MSDN 文档:Writing to a Text File (Visual C#)。
For newer versions of the .NET Framework (Version 2.0. onwards), this can be achieved with one line using the File.WriteAllText
method.
对于较新版本的 .NET Framework(2.0 版以后),可以使用File.WriteAllText
方法在一行中实现。
System.IO.File.WriteAllText(@"C:\TextFile.txt", stringBuilder.ToString());
回答by Tim Schmelter
No need for a StringBuilder
:
不需要一个StringBuilder
:
string path = @"c:\hereIAm.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Here");
sw.WriteLine("I");
sw.WriteLine("am.");
}
}
But of course you can use the StringBuilder
to create all lines and write them to the file at once.
但是当然您可以使用StringBuilder
来创建所有行并将它们一次写入文件。
sw.Write(stringBuilder.ToString());
StreamWriter.Write
Method (String)(.NET Framework 1.1)
StreamWriter.Write
方法(字符串)(.NET Framework 1.1)
回答by Steve
StreamWriteris available for NET 1.1. and for the Compact framework. Just open the file and apply the ToString to your StringBuilder:
StreamWriter可用于 NET 1.1。和Compact 框架。只需打开文件并将 ToString 应用到您的 StringBuilder:
StringBuilder sb = new StringBuilder();
sb.Append(......);
StreamWriter sw = new StreamWriter("\hereIAm.txt", true);
sw.Write(sb.ToString());
sw.Close();
Also, note that you say that you want to append debug messages to the file (like a log). In this case, the correct constructor for StreamWriter is the one that accepts an append
boolean flag. If true then it tries to append to an existing file or create a new one if it doesn't exists.
另请注意,您说要将调试消息附加到文件(如日志)。在这种情况下,StreamWriter 的正确构造函数是接受append
布尔标志的构造函数。如果为 true,那么它会尝试附加到现有文件或创建一个新文件(如果它不存在)。
回答by dscarr
I know this is an old post and that it wants an answer for .NET 1.1 but there's already a very good answer for that. I thought it would be good to have an answer for those people who land on this post that may have a more recent version of the .Net framework, such as myself when I went looking for an answer to the same question.
我知道这是一篇旧帖子,它想要一个 .NET 1.1 的答案,但已经有一个很好的答案。我认为为那些可能拥有更新版本的 .Net 框架的人提供答案会很好,例如我在寻找同一问题的答案时。
In those cases there is an even simpler way to write the contents of a StringBuilder to a text file. It can be done with one line of code. It may not be the most efficient but that wasn't really the question now was it.
在这些情况下,有一种更简单的方法可以将 StringBuilder 的内容写入文本文件。它可以用一行代码完成。它可能不是最有效的,但这并不是现在的问题,对吧。
System.IO.File.WriteAllText(@"C:\MyDir\MyNewTextFile.txt",sbMyStringBuilder.ToString());
回答by Ahmad Alaa
If you need to write line by line from string builder
如果您需要从字符串生成器逐行写入
StringBuilder sb = new StringBuilder();
sb.AppendLine("New Line!");
using (var sw = new StreamWriter(@"C:\MyDir\MyNewTextFile.txt", true))
{
sw.Write(sb.ToString());
}
If you need to write all text as single line from string builder
如果您需要将所有文本从字符串生成器中写为单行
StringBuilder sb = new StringBuilder();
sb.Append("New Text line!");
using (var sw = new StreamWriter(@"C:\MyDir\MyNewTextFile.txt", true))
{
sw.Write(sb.ToString());
}