Simple text writing issue

bigr3d

Member
Joined
May 30, 2007
Messages
6
Programming Experience
Beginner
ok, well i am currently in some trouble; ive looked high and low and my sources (very basic school books) arnt informative enough to tell teach me how to append a text file to separate lines.
my aim is to have a text box save to a pre existing text file, however each line will go under the last text saved.
eg. if i insert "hello" on the first line and click save it will save the text file just as "hello". and if i were to re enter some text such as "goodbye" and save it the text file it was saved to would be

"hello
goodbye"
(without the "")

any help would be greatly appreciated as im in some dire need to complete this IT corse with a teach that has trouble booting her laptop lol
im using vb.net 2005

thanks in advanced!
bigr3d
 
Try this:

VB.NET:
Dim FILE_NAME as String = "C:\test.txt"
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)

objWriter.WriteLine("Hello")
objWriter.WriteLine("Goodbye")

objWriter.Close()

It inserts text into C:\test.txt and uses for each WriteLine a new line (because of the True at the end of the StreamWriter).
If you do not set this True then you will get the following outcome:

Hello
Goodbye

If you give the value False instead of True, VB .NET will create the file for you. It can be very handy if you want logfiles per date.
I use it for my own written Loggingmodule.
 
Try this:

VB.NET:
Dim FILE_NAME as String = "C:\test.txt"
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)

objWriter.WriteLine("Hello")
objWriter.WriteLine("Goodbye")

objWriter.Close()

It inserts text into C:\test.txt and uses for each WriteLine a new line (because of the True at the end of the StreamWriter).
If you do not set this True then you will get the following outcome:

Hello
Goodbye

If you give the value False instead of True, VB .NET will create the file for you. It can be very handy if you want logfiles per date.
I use it for my own written Loggingmodule.

actually the .WriteLine() writes each element of data on it's own line in the file. The "True" in this line: New System.IO.StreamWriter(FILE_NAME, True) is for appending to the current file or start writing at the beginning of the file, the StreamWriter class will always make a new text file if the file doesn't already exist, that part is built into the constructor of the class
 
Back
Top