如何使用VB.Net转到文本文档中的新行

时间:2012-04-10 19:28:40

标签: vb.net

使用

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

如何将第二行文本显示在第一行下,就像我按下Enter键一样?这样做只需将第二行放在第一行旁边。

3 个答案:

答案 0 :(得分:9)

使用Environment.NewLine

File.AppendAllText("c:\mytextfile.text", "This is the first line")
File.AppendAllText("c:\mytextfile.text", Environment.NewLine + "This is the second line")

或者您可以使用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

答案 1 :(得分:3)

如果你有很多这样的调用使用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()) 

如果您要编写许多字符串,则可以将所有字符串包装在方法中。

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

答案 2 :(得分:2)

也许:

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

vbCrLf是换行符的常量。