删除指定的路径文件,如果没有文件则显示msgbox

时间:2017-11-07 02:16:45

标签: vb.net

我想通过询问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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

在这两个解决方案中,你错误地使用了'ElseIf'.corrct方式来做到这一点:

  1. 解决方案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. 解决方案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