Sub ShowFileContents(ByVal strFileName As String)
Dim fs As New FileStream(strFileName, FileMode.Open, FileAccess.Read)
Dim TextFile As New StreamReader(fs)
Me.txtFileContents.Text = Nothing
Dim strLineOfText As String
Do While TextFile.Peek > -1
strLineOfText = TextFile.ReadLine()
Me.txtFileContents.Text = Me.txtFileContents.Text & strLineOfText & vbCrLf
Loop
TextFile.Close()
fs.Close()
End Sub
为什么我在运行程序时无法打开文件,只是说“保存”或“取消”
答案 0 :(得分:2)
为什么我在运行程序时无法打开文件,只是说“保存”或“取消”我想要做的是从我的文档中打开一个文件并将其显示在下面的文本框中。我有保存文件但没有打开文件
看起来您可以使用OpenFileDialog控件而不是文本框来获取要打开的文件路径。
以下是一个例子:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim ofp As New OpenFileDialog
If ofp.ShowDialog = Windows.Forms.DialogResult.OK Then
ShowFileContents(ofp.FileName)
End If
End Sub
Sub ShowFileContents(ByVal strFileName As String)
Dim TextFile As New StreamReader(strFileName)
txtFileContents.Text = Nothing
Do While Not TextFile.EndOfStream
txtFileContents.AppendText(TextFile.ReadLine() & vbCrLf)
Loop
TextFile.Close()
End Sub