Writing to a text file

I am trying to write text to a disk file. I dimensioned a variable, sr, as StreamWriter and used the line sr.WriteLine(strPage) to write the text to the file but all it does is open an empty file and does not write the text to the file. I know there is something in strPage because I also write it to a textbox on my form. Can someone please help

Don



Answer this question

Writing to a text file

  • Herbert Wang

    Hi,

    You can use the sample code snipplet.

    Note that LogData is a string variable and LogFile is a string variable pointing to the path of the file.

    Dim objStreamWriter As StreamWriter

    objStreamWriter = New StreamWriter(LogFile, True, Encoding.Unicode)

    objStreamWriter.WriteLine(LogData)

    objStreamWriter.Close()

    objStreamWriter = Nothing

    Eralper

    http://www.kodyaz.com



  • Dave Abrahams

    You have to make sure that you close the stream before the program ends or you will end up with a blank file.

    Dim sw as new IO.StreamWriter("C:\myFile.txt")

    sw.WriteLine("This is a line of text")

    sw.Close()


  • AppzGuy

    WHat version of VB are you running



  • Kolle

    My system says that "my" is not defined.
  • Flamingwraith12

    Dim tempFile As String = My.Computer.FileSystem.GetTempFileName

    Dim sw As New IO.StreamWriter(New IO.FileStream(tempFile, IO.FileMode.Open Or IO.FileMode.CreateNew))

    sw.WriteLine("hello")

    you can use FileStream to control how you want to open file (Open file, create new file... etc)



  • Psilent Dev

    VB 2005 Express
  • Writing to a text file