File Read/Write Problem

ljs235

Member
Joined
Aug 12, 2003
Messages
6
I am creating a simple program composed on one windows form. The user will navigate the system through a series of buttons. What Im having problems with is creating a log of the users actions. I would like to create a text file that says when they click each button. I tried something like this:

Dim sw As IO.StreamWriter = IO.File.CreateText("user_input.txt")
sw.WriteLine("ButtonA: " & TimeOfDay)
sw.Close()

However, each time it did this, the text file would clear and only the most recent button clicked would appear on the text file.

Ive also tried using appendtext, but I get the same problem. Is what Im trying to do even possible? Thanks in advance for your help. :)
 
Originally posted by ljs235

Dim sw As IO.StreamWriter = IO.File.CreateText("user_input.txt")
sw.WriteLine("ButtonA: " & TimeOfDay)
sw.Close()

I had the SAME exact problem a few days ago. Here Ill get out my project real quick:


At the top of the form (after the windows form generated code) enter this:

[VB]
Private LogWriter As New System.IO.StreamWriter(Application.StartupPath & "/Log.txt", True)
[/VB]

This will open the stream to your Log file thats in the same directory as your application. The TRUE in that part, is so that when you write onto Log.txt, you dont write over whats currently in the file.

Now when you want to write something into Log.txt, go like this:

[VB]
This creates a new line
LogWriter.WriteLine(System.Environment.NewLine)
LogWriter.WriteLine("User enters Button A")
LogWriter.Flush
The flush makes sure that the file gets written
[/VB]

The stream will be open during the whole program time, at which could be bad, I dont know...but what I did, was close the stream during the OnClose event.
 
Thank you both for your help. I was able to solve the problem using your methods. Youve saved me hours of unneeded headaches and I really appreciate it!
 
Back
Top