Reputation: 1124
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.
Upvotes: 6
Views: 43504
Reputation: 46919
Using 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
Using writer As new StreamWriter("mytextfile.text", true)
writer.WriteLine("This is the first line")
writer.WriteLine("This is the second line")
End Using
Upvotes: 10
Reputation: 216273
if you have many of this calls It's way better to use a 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
Upvotes: 3