vb.net - 在VB中处理OpenFileDialog时出错

标签 vb.net error-handling openfiledialog

我是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/

相关文章:

javascript - 错误 : Warning: Accessing non-existent property 'findOne' of module exports inside circular dependency

c# - 应用程序说网络驱动器不存在,但使用 OpenFileDialog 找到

c# - openfiledialog 的过滤属性不起作用

.net - 将 OpenFileDialog.InitialDirectory 设置为 MTP 设备

c# - 重命名具有相同名称不同大小写的目录

VB.NET VScrollBar,反向功能

reactjs - 带有Express服务器的Next.js如何调用自定义错误页面?

Java 多线程错误处理

.net - 将 Windows 服务类添加到类库

c# - 将 Excel 中的数据表列填充为字符串