How to append lines to a file

Using the StreamWriter, the second argument will determine if you want to append to the file.

Dim Sw As StreamWriter = New StreamWriter(filename, True) 'True for appending.
:)
FileOpen is backwards compatibility, avoid it.
 
Visual Basic:
Dim nr As Integer = FreeFile() 'select a freefile
FileOpen(nr, "c:/file.txt", OpenMode.Append) 'open the file c:/file.txt as append
Print(nr, TextBox2.Text) 'write textbox2.text to the file
FileClose(nr) 'close the file if your done
 
No, no, no, that's the depricated way, despite it being correct as the depricated way of doing it! :)
Visual Basic:
Dim DoIwantToAppend As Boolean = True  'purely trivial boolean variable.
Dim SW As StreamWriter = New StreamWriter("C:\file.txt", DoIwantToAppend) 'second parameter is true, so we'll append.
SW.WriteLine(TB.Text)  'write to file.
SW.Close()  'close the stream so changes are made.
 
The FileOpen(), Print(), and FileClose() are old methods and you should avoid them in .NET. These functions are specially meant to be for the older versions of VB, but there are .NET equivalents that do the same thing much more efficiently, namely the StreamWriter methods mentioned above. :)
 
Back
Top