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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:02:55  来源:igfitidea点击:

How to go to new line in a text document using VB.Net

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

回答by vcsjones

Maybe:

也许:

File.AppendAllText("c:\mytextfile.text", "This is the first line")
File.AppendAllText("c:\mytextfile.text", vbCrLf & "This is the second line")

vbCrLfis a constant for a newline.

vbCrLf是换行符的常数。