Skip to main content
 首页 » 编程设计

python之如果尚未打开,请在资源管理器中打开一个文件夹

2025年05月04日7webabcd

我知道如何使用 python 在资源管理器中打开文件夹:

subprocess.Popen(r'explorer/select,"C:\path\of\folder"')

但我不知道如何防止我的程序打开文件夹,如果它已经在资源管理器中“打开”。有没有办法在 Python 中执行此操作(或者可能通过 VBA 脚本)?

请您参考如下方法:

这是我发现的一个有趣的线程,其中使用 VBS 脚本找到了列出打​​开文件夹的工作解决方案,但我不知道如何使用 VBS,因此我无法解决 identifier excepted 错误并使其正常工作..

https://social.msdn.microsoft.com/Forums/vstudio/en-US/de63322b-7f94-4464-be72-2e174106da9c/get-file-explorer-all-opened-folders-path-in-vbnet?forum=vbgeneral

代码本身是:

Imports System.Runtime.InteropServices 

导入 System.Text

公开课Form1 私有(private)常量 WM_GETTEXT 作为整数 = &HD 私有(private)常量 WM_GETTEXTLENGTH 作为整数 = &HE

<DllImport("user32.dll", EntryPoint:="FindWindowExW")> _ 
Private Shared Function FindWindowExW(ByVal hwndParent As System.IntPtr, ByVal hwndChildAfter As System.IntPtr, <InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal lpszClass As String, <InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal lpszWindow As String) As System.IntPtr 
End Function 
 
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ 
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As StringBuilder) As Integer 
End Function 
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    ListBox1.Items.Clear() 
    Dim hWinList As New List(Of IntPtr) 
 
    'Get Each Explorer Windows Handle 
    Dim hWnd As IntPtr = FindWindowExW(IntPtr.Zero, IntPtr.Zero, "CabinetWClass", Nothing) 
    While Not hWnd.Equals(IntPtr.Zero) 
        hWinList.Add(hWnd) 
        hWnd = FindWindowExW(IntPtr.Zero, hWnd, "CabinetWClass", Nothing) 
    End While 
 
    'Loop threw each explorer window in the list and get the text from the Address combobox 
    If hWinList.Count > 0 Then 
        For Each hChld As IntPtr In hWinList 
            Dim hChild1 As IntPtr = FindWindowExW(hChld, IntPtr.Zero, "WorkerW", Nothing) 
            Dim hChild2 As IntPtr = FindWindowExW(hChild1, IntPtr.Zero, "ReBarWindow32", Nothing) 
            Dim hChild3 As IntPtr = FindWindowExW(hChild2, IntPtr.Zero, "ComboBoxEx32", Nothing) 
            Dim len As Integer = SendMessage(hChild3, WM_GETTEXTLENGTH, 0, Nothing) 
            Dim sb As New StringBuilder(len + 1) 
            SendMessage(hChild3, WM_GETTEXT, len + 1, sb) 
            ListBox1.Items.Add(sb.ToString) 
        Next 
    End If 
 
End Sub 

下课