Results 1 to 5 of 5

Thread: [RESOLVED] Appending Text to File In Timer

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    Mar 2001
    Posts
    485

    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.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    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!

  5. #5
    Hyperactive Member
    Join Date
    Mar 2001
    Posts
    485

    Re: [RESOLVED] Appending Text to File In Timer

    Glad that I could be of help.

    I think jmcilhinney maxed out his reps. lol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width