user2087008
user2087008

Reputation:

Streamwriter not adding text to file?

I have the following code:

If line = Nothing Then
    MsgBox("Login failed.")
        Using sw As New StreamWriter(File.Open(Index.strLogPath, FileMode.OpenOrCreate)) ' open or create a new file at our path
        sw.BaseStream.Seek(0, SeekOrigin.End) ' append new users to the end
        sw.AutoFlush = True
        sw.WriteLine("line is nothing")
        sw.Flush()
        sw.Close()
        End Using

End If

My line = nothing condition is met, the msgbox pops up letting me know, but the file is not created. If the file is there, nothing is added to it.

I have checked the path validity, and ensured that applications have permission there, everything I can think of isn't working, and to make it more frustrating there are no errors! Any help would be greatly appreciated.

Upvotes: 0

Views: 144

Answers (1)

Steven Liekens
Steven Liekens

Reputation: 14088

Did you know that the File class already has a utility method to do exactly that?

File.AppendAllText(Index.strLogPath, "line is nothing")

Should be as simple as that. :)

EDIT

If you insist on managing the file stream yourself, try this:

    Using sw As New StreamWriter(File.Open(Index.strLogPath, FileMode.Append)) ' open or create a new file at our path
        sw.WriteLine("line is nothing")
    End Using

Points of interest:

  • Use FileMode.Append instead of OpenOrCreate
  • No need to flush or close. This is done automatically when you leave the "using" block.

Upvotes: 1

Related Questions