OpenFileDialog (Cancel/Close) 找不到文件崩溃
OpenFileDialog (Cancel/Close) File Not Found Crash
这是我的代码:
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:DesktopFILE\RobeVisualStudio\ShaadyyTool\bin\Debug\Select a Text File' Not Found.
我想确保那些关闭 "OpenFileDialog1" 应用程序的人不会崩溃。
我不知道为什么它不起作用,
你能写出正确的代码谢谢。
对不起我的工程师。
openFileDialog1.FileName = "Select a Text File..."
我想你没有理解上面的语句设置为对话框的标题...这不是你想的...而是用于find/select打开目录中的文件
您想对此进行一些更改。使用对话结果来确定您是否应该继续前进。如果他们没有 select 文件,请不要执行代码。
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
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 使用非托管代码,您确实应该将其放在 Using
块中。
Using openFileDialog1 as OpenFileDialog = New OpenFileDialog
openFileDialog1.Filter = "Text Files (*.txt) | *txt"
...
End Using
这样你就可以确保它完全从内存中删除,即使 Using
块中有异常。
那么你应该给你的 OpenFileDialog 一个元素,它将以模式打开,否则你的 OpenFileDialog 可能会在后台消失并且你的应用程序似乎没有响应(因为它等待 OpenFileDialog return 但是用户看不到)。
既然你似乎在 Form
中,就做
openFileDialog1.ShowDialog(Me)
那么您应该检查 openFileDialog1.ShowDialog()
的 return 值,只有在确实选择了文件时才继续。
If openFileDialog1.ShowDialog(Me) = DialogResult.Ok Then
...
End If
如果 openFileDialog1.ShowDialog()
的结果不是 DialogResult.Ok
那么 openFileDialog1.FileName
可能是 Nothing
.
希望对您有所帮助。
这是我的代码:
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:DesktopFILE\RobeVisualStudio\ShaadyyTool\bin\Debug\Select a Text File' Not Found.
我想确保那些关闭 "OpenFileDialog1" 应用程序的人不会崩溃。 我不知道为什么它不起作用, 你能写出正确的代码谢谢。 对不起我的工程师。
openFileDialog1.FileName = "Select a Text File..."
我想你没有理解上面的语句设置为对话框的标题...这不是你想的...而是用于find/select打开目录中的文件
您想对此进行一些更改。使用对话结果来确定您是否应该继续前进。如果他们没有 select 文件,请不要执行代码。
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
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 使用非托管代码,您确实应该将其放在 Using
块中。
Using openFileDialog1 as OpenFileDialog = New OpenFileDialog
openFileDialog1.Filter = "Text Files (*.txt) | *txt"
...
End Using
这样你就可以确保它完全从内存中删除,即使 Using
块中有异常。
那么你应该给你的 OpenFileDialog 一个元素,它将以模式打开,否则你的 OpenFileDialog 可能会在后台消失并且你的应用程序似乎没有响应(因为它等待 OpenFileDialog return 但是用户看不到)。
既然你似乎在 Form
中,就做
openFileDialog1.ShowDialog(Me)
那么您应该检查 openFileDialog1.ShowDialog()
的 return 值,只有在确实选择了文件时才继续。
If openFileDialog1.ShowDialog(Me) = DialogResult.Ok Then
...
End If
如果 openFileDialog1.ShowDialog()
的结果不是 DialogResult.Ok
那么 openFileDialog1.FileName
可能是 Nothing
.
希望对您有所帮助。