Search code examples
vb.netfilestreamstreamreaderopenfiledialog

Storing a selected notepad its contents to FileStream


I am currently having problems in storing the contents of any selected notepad file from openFileDialog to the FileStream. My code states a default filename as i do not know how to revise the FileStream code, i want it to register the filename of my selected notepad. By default it would only read the contents of "messages.txt", i want to have a freedom to choose any notepad file and retrieve the data from it. Any kind of help or advice will do thanks in advance.

here is my code:

    Dim Stream As New System.IO.FileStream("messages.txt", IO.FileMode.Open) 
    'i need to do something about this line above
    Dim sReader As New System.IO.StreamReader(Stream)
    Dim Index As Integer = 0

    Dim openFileDialog1 As New OpenFileDialog()
    openFileDialog1.InitialDirectory = "D:\work"
    openFileDialog1.Filter = "txt files (*.txt)|*.txt"
    openFileDialog1.FilterIndex = 2
    openFileDialog1.RestoreDirectory = True

    If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Try
            Stream = openFileDialog1.OpenFile()
            If (Stream IsNot Nothing) Then

                Do While sReader.Peek >= 0
                    ReDim Preserve eArray(Index)
                    eArray(Index) = sReader.ReadLine
                    RichTextBox3.Text = eArray(Index)
                    Index += 1
                    Delay(2)
                Loop

            End If
        Catch Ex As Exception
            MessageBox.Show(Ex.Message)
        Finally
            If (Stream IsNot Nothing) Then
                Stream.Close()
            End If
        End Try
    End If

End Sub

Solution

  • I rearranged your code just a bit. Basically you were creating a stream, and then creating the reader for the stream to "messages.txt". Later you set the stream object to a new stream based on the OpenFile method of the file open dialog. But your reader was still pointing to the first stream you opened, so that is what was getting read. I've changed the code below to not open that first stream, and to create the reader after you have created the stream based on the file the user selected.

    'don't create a stream hear and the reader, because you are just recreating a stream later in the code
    Dim Stream As System.IO.FileStream
    
    Dim Index As Integer = 0
    
    Dim openFileDialog1 As New OpenFileDialog()
    openFileDialog1.InitialDirectory = "D:\work"
    openFileDialog1.Filter = "txt files (*.txt)|*.txt"
    openFileDialog1.FilterIndex = 2
    openFileDialog1.RestoreDirectory = True
    
    If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Try
            'This line opens the file the user selected and sets the stream object
            Stream = openFileDialog1.OpenFile()
            If (Stream IsNot Nothing) Then
                'create the reader here and use the stream you got from the file open dialog
                Dim sReader As New System.IO.StreamReader(Stream)
                Do While sReader.Peek >= 0
                    ReDim Preserve eArray(Index)
                    eArray(Index) = sReader.ReadLine
                    RichTextBox3.Text = eArray(Index)
                    Index += 1
                    Delay(2)
                Loop
    
            End If
        Catch Ex As Exception
            MessageBox.Show(Ex.Message)
        Finally
            If (Stream IsNot Nothing) Then
                Stream.Close()
            End If
        End Try
    End If
    
    End Sub