Question

I want to create a button in VB.net that lets me browse my hard drive for the specified notepad file i want to open and retrieve the contents from it, i only have tried using FileStream and StreamReader but this wont let me manually select the notepad file instead i have to declare a default filename. Any sample codes would be appreciated thanks in advance, i just need a starting point. I am really stuck to this.

This the code i am using right now, but i have to specify the correct file name on it:

        Dim fStream As New System.IO.FileStream("messages.txt", IO.FileMode.Open)
        Dim sReader As New System.IO.StreamReader(fStream)
        Dim Index As Integer = 0
        Do While sReader.Peek >= 0
            ReDim Preserve sArray(Index)
            sArray(Index) = sReader.ReadLine
            Index += 1
        Loop
Was it helpful?

Solution 2

I think you may be using the wrong approach with a FileStream. Instead look to allow a user to select a file, then use Process.Start to open Notepad.

Take a look here for examples on selecting a file. The page here then details Process.Start.

I'm happy to provide more code samples directly here, but those two pages should be sufficient.

OTHER TIPS

If I understand your question correctly, you want to have an option to choose which textfile to open, if so you can try this:

Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
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 your loop here
        End If
    Catch Ex As Exception
        MessageBox.Show(Ex.Message)
    Finally
        If (stream IsNot Nothing) Then
            stream.Close()
        End If
    End Try
End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top