OpenFileDialog(取消/关闭)文件未找到崩溃

时间:2018-01-24 12:38:32

标签: vb.net openfiledialog

这是我的代码:

Private Sub HuraButton1_Click(sender As Object, e As EventArgs) Handles HuraButton1.Click
    Dim openFileDialog1 As New OpenFileDialog()
    openFileDialog1.FileName = "Select a Text File..."
    openFileDialog1.Filter = "Text Files (*.txt) | *txt"
    OpenFileDialog1.InitialDirectory = "C:\Users\Public\Desktop\"
    OpenFileDialog1.Title = "Select a Files"
    openFileDialog1.ShowDialog()

    Dim Findstring = IO.File.ReadAllText(openFileDialog1.FileName)
    Dim Lookfor As String = ""
    Dim result = openFileDialog1.ShowDialog()
    If openFileDialog1.FileName = Windows.Forms.DialogResult.Cancel Then
        MsgBox("File Not Found")
    End If
    If Findstring.Contains(Lookfor) Then
        MsgBox("Found")
    Else
        MsgBox("Not Found")
    End If

End Sub

错误:

System.IO.FileNotFoundException: The File'D:\1DesktopFILE\RobeVisualStudio\ShaadyyTool\bin\Debug\Select a Text File' Not Found.

我想确保那些关闭" OpenFileDialog1"该应用程序没有崩溃。 我不知道它为什么不起作用, 你能写出正确的代码吗? 对不起我的工作。

4 个答案:

答案 0 :(得分:0)

openFileDialog1.FileName = "Select a Text File..."

我想您错过了将上述语句设置为对话框的标题...这不是您的想法...而是用于在打开的目录中查找/选择文件

答案 1 :(得分:0)

您想对此进行一些更改。使用对话框结果确定是否应该继续前进。如果他们没有选择文件,请不要执行此操作。

If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

    Dim Findstring as String = IO.File.ReadAllText(openFileDialog1.FileName)
    Dim Lookfor As String = ""

    If Findstring.Contains(Lookfor ) Then
        MsgBox("Found")
    Else
        MsgBox("Not Found")
    End If

End If

答案 2 :(得分:0)

  

我认为这就是你想要的

Private Sub HuraButton1_Click(sender As Object, e As EventArgs) Handles HuraButton1.Click
    Dim openFileDialog1 As New OpenFileDialog()
    openFileDialog1.Filter = "Text Files (*.txt) | *txt"
    openFileDialog1.InitialDirectory = "C:\Users\Public\Desktop\"
    openFileDialog1.Title = "Select a Files"
    openFileDialog1.CheckFileExists = True
    If openFileDialog1.ShowDialog() = DialogResult.OK Then
        'file selected now process it 
        Dim Findstring = IO.File.ReadAllText(openFileDialog1.FileName)
        Dim Lookfor As String = ""
        If Findstring.Contains(Lookfor) Then
            MsgBox("Found")
        Else
            MsgBox("Not Found")
        End If
    Else
        'file not selected or user cancelled 
        MsgBox("file not selected")
    End If
End Sub

答案 3 :(得分:0)

由于OpenFileDialog使用非托管代码,因此您应该将其放在Using块中。

Using openFileDialog1 as OpenFileDialog = New OpenFileDialog
    openFileDialog1.Filter = "Text Files (*.txt) | *txt"
    ...
End Using

这样,即使Using块内存在异常,也可以确保它已完全从内存中删除。

然后你应该给你的OpenFileDialog一个元素,它将以模态方式打开,否则你的OpenFileDialog会在后台消失而你的应用程序似乎没有响应(因为它等待OpenFileDialog返回但用户看不到那)。 既然你似乎还在Form,那就去做

openFileDialog1.ShowDialog(Me)

然后你应检查openFileDialog1.ShowDialog()的返回值,并且只有在确实选择了文件时才会继续。

If openFileDialog1.ShowDialog(Me) = DialogResult.Ok Then
    ...
End If

如果openFileDialog1.ShowDialog()的结果不是DialogResult.Ok,那么openFileDialog1.FileName可能是Nothing

我希望这会有所帮助。