我是VB的新手,所以我很欣赏这可能是一个愚蠢的问题。
我正在使用以下代码从本地文件系统中选择CSV文件。
Dim openFileDialog1 As New OpenFileDialog
openFileDialog1.InitialDirectory = "c:\uploads"
openFileDialog1.Filter = "CSV files (*.csv)|*.csv"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
End If
'--TextField Parser is used to read the files
Dim parser As New FileIO.TextFieldParser(openFileDialog1.FileName)
如果未选择文件,如何正常显示此错误。
因此,实质上显示一条消息,指出“未选择文件”
先感谢您。
最佳答案
Dim openFileDialog1 As New OpenFileDialog
Dim strFileName As String
openFileDialog1.InitialDirectory = "c:\uploads"
openFileDialog1.Filter = "CSV files (*.csv)|*.csv"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
strFiilename = openFileDialog1.FileName
If strFileName <> "" Then
'file is selected
Dim parser As New FileIO.TextFieldParser(strFiilename)
Else
MsgBox("No File Selected")
End If
End If
我认为无需检查此
strFileName
,因为在您的问题中您已放置Dim parser As New FileIO.TextFieldParser(openFileDialog1.FileName)
后
If openFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
endif
这将导致您的错误
关于vb.net - 在VB中处理OpenFileDialog时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23337517/