Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am creating a simple program composed on one windows form. The user will navigate the system through a series of buttons. What I'm having problems with is creating a log of the user's 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.

 

I've also tried using appendtext, but I get the same problem. Is what I'm trying to do even possible? Thanks in advance for your help. :)

Posted

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 I'll get out my project real quick:

 

 

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

 

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

 

This will open the stream to your Log file that's in the same directory as your application. The TRUE in that part, is so that when you write onto Log.txt, you don't write over what's currently in the file.

 

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

 

'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

 

The stream will be open during the whole program time, at which could be bad, I don't know...but what I did, was close the stream during the OnClose event.

"Reality is fake, Dreams are for real"
Posted
Thank you both for your help. I was able to solve the problem using your methods. You've saved me hours of unneeded headaches and I really appreciate it!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...