Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用VB.Net在文本文档中转到新行_Vb.net - Fatal编程技术网

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

如何使用VB.Net在文本文档中转到新行,vb.net,Vb.net,使用 如何使第二行文本显示在第一行下,就好像我按了回车键一样?这样做只会将第二行放在第一行的旁边。也许: File.AppendAllText("c:\mytextfile.text", "This is the first line") File.AppendAllText("c:\mytextfile.text", "This is the second line") 是换行符的常量。使用环境。换行符 File.AppendAllText("c:\mytextfile.text", "Th

使用

如何使第二行文本显示在第一行下,就好像我按了回车键一样?这样做只会将第二行放在第一行的旁边。

也许:

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

是换行符的常量。

使用
环境。换行符

File.AppendAllText("c:\mytextfile.text", "This is the first line")
File.AppendAllText("c:\mytextfile.text", vbCrLf & "This is the second line")
或者您可以使用
StreamWriter

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

如果您有很多这样的调用,最好使用StringBuilder:

Using writer As new StreamWriter("mytextfile.text", true)
    writer.WriteLine("This is the first line")
    writer.WriteLine("This is the second line")
End Using
如果您有很多字符串要写,那么您可以将所有内容包装在一个方法中

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()) 

这意味着在内存中处理完整的字符串(取决于大小)可能会有问题。@Magnus是的,但可以通过一些对AppendAllText的中间调用轻松处理。查看我的最新答案
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