Logging to a Text File

JDYoder

Well-known member
Joined
Nov 18, 2003
Messages
144
I need to keep a log file of activity in a simple text file. Ive done this in VB6, but Im guessing VB.NET has a single object I can use to write individual lines to a text file. Or is that not the case? Thanks for any recommendations you can give.
 
If I declare variables as either of those types, I see there are "Write" commands and other stuff, but Im not seeing how you assign a particular file to either of them to serve as the log file.
 
Not sure if this helps, but this is how I log stuff to text files.

[VB]

Dim Datawrite As StreamWriter
Datawrite = New StreamWriter("C:/Test.txt")
Datawrite.WriteLine("Hello World")
Datawrite.Close()

[/VB]

-=Simcoder=-
 
That looks like itll work. Thanks for the starting point.

BTW, I cant figure out your avatar... what is it? Some mythical being playing a flute?
 
Lol, actually its a Navy Seal holding a gun. Its from a game called Counter-Strike. I just made him change colors =p

-=Simcoder=-
 
Ah. I can kind of see it now. hehe.

Your code worked for me, but I see it always recreates the file. How can I tweak it so that it will append instead?
 
Code:
dim fs as filestream
fs = new filestream("C:\test.txt",filemode.append)
dim sw as streamwriter
sw = new streamwriter(fs)
sw.writeline("Poo")
sw.close
fs.close

That should do it. Check the intellisense when you type filemode. Youll get quite a few of options there.

Also, streamreaders work very similarly to streamwriters.

EDIT: Oh! filestream,streamwriter,streamreader and filemode are all things in the system.io namespace. So, they should be io.filestream, io.streamwriter, etc if you didnt import io.
 

Similar threads

Back
Top