读书人

VB 打开对话框,该怎么解决

发布时间: 2012-12-17 09:31:40 作者: rapoo

VB 打开对话框

Public Class Form1
Private strFileName As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
With OpenFileDialog1
.Filter = "Text Documents (*.txt)|*.txt|All Files(*.*)|*.*"
.FilterIndex = 1
.Title = "demo open file"
End With

If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then


Try
strFileName = OpenFileDialog1.FileName
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText("strFileName")
TextBox1.Text = fileContents
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
End Class




看着教程来的, 点击按钮弹出打开对话框,打开一个TXT文档,并将内容显示在TEXTBOX中
为何运行时提示未能找到文件“C:\Users\lrp\Documents\Visual Studio 2010\Projects\OpenDlg\OpenDlg\bin\Debug\strFileName"
strFileName是我定义的啊



[最优解释]
fileContents = My.Computer.FileSystem.ReadAllText("strFileName")
=>
fileContents = My.Computer.FileSystem.ReadAllText(strFileName)
[其他解释]

没人回复

[其他解释]
貌似应该去找这个文件:
"C:\Users\lrp\Documents\Visual Studio 2010\Projects\OpenDlg\OpenDlg\bin\Debug\" & strFileName

读书人网 >VB

热点推荐