Wednesday, November 24, 2010

File Handling (StreamReader and StreamWriter)

" Before the coding procedure, you have to place 2 buttons and 1 Richtextbox in the form. "



Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If (System.IO.File.Exists("F:\MyProgram.c")) Then

            Dim a As New System.IO.StreamReader("F:\MyProgram.c")
            Dim b As String

            If (RichTextBox1.Text = "") Then
                b = a.ReadLine()

                Do Until b Is Nothing
                    RichTextBox1.Text = RichTextBox1.Text + b
                    b = a.ReadLine()
                Loop
              
            End If
            a.Close()

        Else

            MsgBox("This file does not exist, Plz write in RTB")

        End If

    End Sub



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim c As New System.IO.StreamWriter("F:\MyProgram.c")
        Dim d As String

        c.WriteLine(RichTextBox1.Text)
        c.Close()

    End Sub

End Class