我想在VB.net中创建一个按钮,让我浏览我的硬盘以获取指定的记事本文件我想打开并从中检索内容,我只是尝试过使用FileStream和StreamReader,但这不会让我手动选择记事本文件而不是声明默认文件名。任何样本代码将提前感谢,我只需要一个起点。我真的很坚持这个。
这是我现在正在使用的代码,但我必须在其上指定正确的文件名:
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
答案 0 :(得分:1)
如果我理解你的问题,你想要选择打开哪个文本文件,如果是这样你可以试试这个:
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
答案 1 :(得分:0)