[RESOLVED] Appending Text to File In Timer
Hello everyone!
Stuff like this makes me hate computers! Anyways, I'm again struggling with something relatively simple - or so i thought.
I have a sub:
Code:
Private Sub WriteToFile()
Dim stream_writer As StreamWriter
stream_writer = File.AppendText("Results.txt")
stream_writer.WriteLine(strName)
stream_writer.Close()
End Sub
Then, I have a sub that runs every minute, calling yet another sub, which calls this one. Before you say whoa! that's too many, I'll say that each of these subs has a different purpose.
The whole problem is that the WriteToFile sub writes only once to a file! Instead of continously writing to the file every minute, it stops at the first line.
How can i achieve this?
Re: Appending Text to File In Timer
Why not get rid of your WriteToFile method altogether and just call File.AppendAllText?
That said, that method is not going to "stop writing" to the file. If the method is called it will do what it's supposed to do. Either the method isn't being called or you're not looking for the data correctly. One possible explanation is the fact that you aren't specifying a path for the file (bad, bad, bad) and the current working directory for the application is changing, causing the subsequent data to be written to a file in a different location. In that case the way to fix it is to ALWAYS specify the full path of the file. If you want the file in the same folder as the EXE then say so; don't rely on the current directory being that folder.
EDIT: Just noticed that you're using .NET 1.x, so the File.AppendAllText suggestion doesn't apply. I'd suggest upgrading to a newer version if it's practical, which I realise it may not be.
Re: Appending Text to File In Timer
Code:
stream_writer.WriteLine(strName)
Can you confirm to see if your strName got updated every minute? What if it's empty string, hence there's nothing written to the file?
Seems fairly fine to me, as long as your strName contains a value.
Re: Appending Text to File In Timer
Thank you again guys!
jmcilhinney, I still can't add rep to you :(
It seems as if I never updated my strName variable, that is what i think the problem was. it is solved now thank you!
Re: [RESOLVED] Appending Text to File In Timer
Glad that I could be of help.
I think jmcilhinney maxed out his reps. lol