vb.net 如何使用VB.Net转到文本文档中的新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10095155/
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
How to go to new line in a text document using VB.Net
提问by Pickle
Using
使用
File.AppendAllText("c:\mytextfile.text", "This is the first line")
File.AppendAllText("c:\mytextfile.text", "This is the second line")
How do I make the second line of text appear under the first one, as if I hit the Enter key? Doing it this way simply puts the second line right next to the first line.
如何使第二行文本出现在第一行下方,就像我按 Enter 键一样?这样做只是将第二行放在第一行旁边。
回答by Magnus
Using Environment.NewLine
使用 Environment.NewLine
File.AppendAllText("c:\mytextfile.text", "This is the first line")
File.AppendAllText("c:\mytextfile.text", Environment.NewLine + "This is the second line")
Or you can use the StreamWriter
或者你可以使用 StreamWriter
Using writer As new StreamWriter("mytextfile.text", true)
writer.WriteLine("This is the first line")
writer.WriteLine("This is the second line")
End Using
回答by Steve
if you have many of this calls It's way better to use a StringBuilder:
如果您有很多这样的调用,最好使用 StringBuilder:
Dim sb as StringBuilder = New StringBuilder()
sb.AppendLine("This is the first line")
sb.AppendLine("This is the second line")
sb.AppendLine("This is the third line")
....
' Just one call to IO subsystem
File.AppendAllText("c:\mytextfile.text", sb.ToString())
If you have really many many strings to write then you could wrap everything in a method.
如果您真的有很多字符串要编写,那么您可以将所有内容都包装在一个方法中。
Private Sub AddTextLine(ByVal sb As StringBuilder, ByVal line as String)
sb.AppendLine(line)
If sb.Length > 100000 then
File.AppendAllText("c:\mytextfile.text", sb.ToString())
sb.Length = 0
End If
End Sub