读书人

VB 打开对话框,该如何处理

发布时间: 2013-01-08 14:02:13 作者: 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)

读书人网 >VB

热点推荐