Pages

Sunday, 8 July 2012

Vb.net Read and Write Text File

This tutorial is going to teach to how to read and writing data from a Text file. We use StreamReader to read and StreamWriter to write date on Text file. In example given below, we using TextBox1.Text to get the full address of the Text file. So, Before writing this code you have to insert TextBox in your form.


Public Sub Readwrite()

        If System.IO.File.Exists(TextBox1.Text) = True Then
            Dim myobjreader As New System.IO.StreamReader(TextBox1.Text)
            Dim str As String = myobjreader.ReadToEnd
            MsgBox(str)
            myobjreader.Close()

            Dim myobjwriter As New System.IO.StreamWriter(TextBox1.Text)
            myobjwriter.Write(str & "This file is write using VB.NET.")
            myobjwriter.Close()
        Else
            MsgBox("File Does Not Exist !")
        End If

End Sub

No comments:

Post a Comment