一次关闭多个文件夹

时间:2017-04-23 13:55:21

标签: vb.net

我正在将这部分代码用于vb.net项目以关闭三个特定文件夹,但只有两个关闭。知道为什么会这样吗?

Private Sub CloseFolders_Button_MouseClick(sender As Object, e As MouseEventArgs) Handles CloseFolders_Button.MouseClick
    Dim FrapsFolder_C As String = FrapsFolder_TextBox.Text
    Dim DestVideoFolder_C As String = DestVideoFolder_TextBox.Text
    Dim DestImagesFolder_C As String = DestImagesFolder_TextBox.Text
    Dim sh As Object = CreateObject("shell.application")

    For Each w In sh.Windows
        If w.document.folder.self.Path = FrapsFolder_C Or
            w.document.folder.self.Path = DestVideoFolder_C Or
            w.document.folder.self.Path = DestImagesFolder_C Then
            w.Quit
        End If
    Next
End Sub

1 个答案:

答案 0 :(得分:1)

这是一个非常奇怪的问题。我浪费了大量的时间试图解决没有结果。但是,我尽力解决这个问题。

我只是将主代码重新编写成子程序,以便根据打开的文件夹数量多次使用它。

Sub CloseFolders(folder1 As String, folder2 As String, folder3 As String)
    Try
        For Each w In sh.windows
          ' I used select .. case instead of if statement for readability 
            Select Case w.document.folder.self.Path
                Case folder1, folder2, folder3
                    w.Quit()
            End Select
        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

然后在点击事件中,您可以调用此子例程,如下所示:

    For i As Integer = sh.windows.count - 1 To 1 Step -1
        CloseFolders(FrapsFolder_TextBox.Text, DestVideoFolder_TextBox.Text, DestImagesFolder_TextBox.Text)
    Next

不要忘记声明并创建shell

Dim sh As Object = CreateObject("shell.application")

我期待找到更好的解决方案。我认为使用进程而不是shell,我不确定它是否会工作,但我更愿意尽可能接近原始代码。