level 1
a一行
楼主
我想利用vb.net打开一个文本文档,关于openfiledialog控件代码如下:
Private Sub btnopen_Click(sender As Object, e As EventArgs) Handles btnopen.Click
OpenFileDialog1.Filter = "text documents (*.txt)|*.txt|all files (*.*)|*.*"
OpenFileDialog1.FilterIndex = 1
OpenFileDialog1.Title = "demo open file dialog"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
strfilename = OpenFileDialog1.FileName
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText("strfilename")
txtfile.Text = fileContents
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
但是运行后可以弹出如下的文件选择对话框:

但是选中该文本文件后单击“打开”按钮,就会蹦出一个无法找到文件的对话框,如下所示:

哪位大神能告诉我哪里出问题了,感激不尽
2016年03月21日 06点03分
1
Private Sub btnopen_Click(sender As Object, e As EventArgs) Handles btnopen.Click
OpenFileDialog1.Filter = "text documents (*.txt)|*.txt|all files (*.*)|*.*"
OpenFileDialog1.FilterIndex = 1
OpenFileDialog1.Title = "demo open file dialog"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
strfilename = OpenFileDialog1.FileName
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText("strfilename")
txtfile.Text = fileContents
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
但是运行后可以弹出如下的文件选择对话框:

但是选中该文本文件后单击“打开”按钮,就会蹦出一个无法找到文件的对话框,如下所示:
哪位大神能告诉我哪里出问题了,感激不尽