Ad

Need Helping In Writing Text Files In VB

- 1 answer

I am trying to write code to write text files in VB. So far, I have this and Visual Studio is coming up with some errors. I have 2 errors on my code and I have an "Else" must be proceeding by a matching "if" and a "end if" must be proceeding matching "if" error.

 Private Sub btnload_Click(sender As System.Object, e As System.EventArgs) Handles btnload.Click

        Dim FILE_NAME As String = "test.txt"
        If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME)
        txtload.Text = objReader.ReadToEnd.objReader.Close()
        objReader.Close()


        Else
        MsgBox("File Does Not Exist")
        End If

    End Sub
Ad

Answer

A couple things:

  • your file needs a system path
  • if you just want to write to a text file, add text to it -you don't need to read it in

Try writing directly to the file:

My.Computer.FileSystem.WriteAllText("C:\TestFolder1\test.txt",

"This is new text to be added.",True)

Ad
source: stackoverflow.com
Ad