我想通过询问MsgBoxStyle.YesNo删除指定目录中的文件,如果这些文件不存在,则显示msgbox。
我尝试了以下代码,但只打开了MsgBoxStyle.YesNo,没有其他事情发生。
Dim di As IO.DirectoryInfo = New IO.DirectoryInfo(Application.StartupPath & "\..\..\1.txt")
Dim di1 As IO.DirectoryInfo = New IO.DirectoryInfo(Application.StartupPath & "\..\..\2.txt")
Dim di2 As IO.DirectoryInfo = New IO.DirectoryInfo(Application.StartupPath & "\..\..\3.txt")
If MsgBox("Are you sure?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
ElseIf di.Exists And di1.Exists And di2.Exists Then
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\..\..\1.txt")
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\..\..\2.txt")
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\..\..\3.txt")
MsgBox("Done!", MsgBoxStyle.Exclamation)
Else
MsgBox("No file found!", MsgBoxStyle.Exclamation)
End If
我也试过以下代码。但是这次如果文件不存在,应用程序崩溃。
Dim di As IO.DirectoryInfo = New IO.DirectoryInfo(Application.StartupPath & "\..\..\1.txt")
Dim di1 As IO.DirectoryInfo = New IO.DirectoryInfo(Application.StartupPath & "\..\..\2.txt")
Dim di2 As IO.DirectoryInfo = New IO.DirectoryInfo(Application.StartupPath & "\..\..\3.txt")
If di.Exists And di1.Exists And di2.Exists Then
ElseIf MsgBox("Are you sure?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\..\..\1.txt")
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\..\..\2.txt")
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\..\..\3.txt")
MsgBox("Done!", MsgBoxStyle.Exclamation)
Else
MsgBox("No file found!", MsgBoxStyle.Exclamation)
End If
我做错了什么?
答案 0 :(得分:0)
在这两个解决方案中,你错误地使用了'ElseIf'.corrct方式来做到这一点:
解决方案1:
If MsgBox("Are you sure?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
If di.Exists And di1.Exists And di2.Exists Then
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\..\..\1.txt")
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\..\..\2.txt")
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\..\..\3.txt")
MsgBox("Done!", MsgBoxStyle.Exclamation)
Else
MsgBox("No file found!", MsgBoxStyle.Exclamation)
End If
End If
解决方案2:
If di.Exists And di1.Exists And di2.Exists Then
If MsgBox("Are you sure?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\..\..\1.txt")
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\..\..\2.txt")
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\..\..\3.txt")
MsgBox("Done!", MsgBoxStyle.Exclamation)
Else
MsgBox("No file found!", MsgBoxStyle.Exclamation)
End If