Imports System.IO

Public Class DemoReaderWriter

    Inherits System.Windows.Forms.Form

    Dim fStream As New FileStream("C:\VBTutorial.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)

    Dim SWriter As New StreamWriter(fStream)

    Public Sub WriteToFile(ByVal txt As String, ByVal sWriter As StreamWriter)

        sWriter.WriteLine(txt)

        sWriter.WriteLine("")

        sWriter.Flush()

    End Sub

    Public Sub readTxt(ByVal sR As StreamReader)

        Me.RichTextBox1.Text = sR.ReadToEnd

    End Sub

 

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

        Dim sR As New StreamReader(fStream)

        sR.BaseStream.Seek(0, SeekOrigin.Begin)

        Me.readTxt(sR)

        Me.Button1.Enabled = True

        Me.Button2.Enabled = False

 

    End Sub

 

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

        SWriter.BaseStream.Seek(0, SeekOrigin.End)

        Me.WriteToFile(Me.RichTextBox1.Text, SWriter)

        Me.Button2.Enabled = True

        Me.Button1.Enabled = False

        Me.RichTextBox1.Text = ""

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Me.RichTextBox1.Text = ""

    End Sub

 

    Private Sub DemoReaderWriter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Button2.Enabled = False

    End Sub

End Class