未找到OpenFileDialog(取消/关闭)文件崩溃 [英] OpenFileDialog (Cancel/Close) File Not Found Crash

查看:70
本文介绍了未找到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"应用程序的用户不会崩溃. 我不知道为什么它不起作用, 您能写出正确的代码吗? 对不起,我的英文.

I want to make sure that those who close the "OpenFileDialog1" The app doesn't crash. I Don't know why it doesn't work, Can you write the correct code thanks. Sorry for my eng.

推荐答案

我认为这就是您想要的

I think this is what you want

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

这篇关于未找到OpenFileDialog(取消/关闭)文件崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆