我知道如何在资源管理器与Python通过打开文件夹:在资源管理器中打开一个文件夹,如果尚未打开
subprocess.Popen(r'explorer /select,"C:\path\of\folder"')
但我不知道如何阻止我的程序从打开如果文件夹它已经在浏览器中“打开”了。有没有办法在Python中做到这一点(或者通过VBA脚本)?
我知道如何在资源管理器与Python通过打开文件夹:在资源管理器中打开一个文件夹,如果尚未打开
subprocess.Popen(r'explorer /select,"C:\path\of\folder"')
但我不知道如何阻止我的程序从打开如果文件夹它已经在浏览器中“打开”了。有没有办法在Python中做到这一点(或者通过VBA脚本)?
我想你的问题主要是由于用户重新打开或重新点击使用你的应用程序。你可以在浏览器中你开了一个变量的PID(进程-id)存储
::部分做的也可能是
一种方式subprocess.Popen(r'explorer /select,"C:\path\of\folder"')
然后,当试图再次打开,如果一个pid被设置。检查过程是否仍然有效。如果是的话,那么不要打开它,也许找到一种方法来专注于打开的寡妇。
伟大的想法没有实现它的每一个解决方案可能是有用的。如果用户在程序之前打开文件夹,那么仍然存在问题,然后该程序将不具有该pid。 – 83101118101110
是的,这是限制。我不知道是否有办法做得更好。 –
我不知道你要什么,但也许这样的事情会帮助:
import os
for root, dirs, files in os.walk(Folder_Root, topdown=False):
for name in dirs:
full_path = os.path.join(root, name)
#use Popen to open the folder here
所以通过所有Folder_Root下的目录读取,打开每一个与POPEN。每个文件夹只能打开一次。只需将Folder_Root替换为实际路径即可。
我想要做的是防止我的程序打开一个文件夹,如果它已被用户在资源管理器中打开,不打开文件夹的每个子目录。 – 83101118101110
这个线程可能会有所帮助:https://stackoverflow.com/questions/12726218/name-of-files-opened-by-a-process-in-window – theObserver
使用psutil似乎是解决问题的好办法,它擅长查找运行的二进制文件,但由于它们不是进程,因此打开文件夹比较困难。解决方案可能是寻求进程“explorer.exe”,但我没有找到一个正确的方法来做到这一点。 – 83101118101110
这里有一个有趣的线程,我发现其中一个工作解决方案列出打开的文件夹与VBS脚本找到,但我不知道如何使用VBS,因此我无法解决的错误和identifier excepted
它正常工作..
代码本身是:
Imports System.Runtime.InteropServices
进口System.Text
公共类Form1中 私人常量WM_GETTEXT为整数= & HD 私人常量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
末级
很好的问题,但并没有真正涉及到蟒蛇 –
我想实现它在python,但它是一个相当普遍的问题的确,即使使用python如果用户试图在程序已经打开,打开文件夹解决问题 – 83101118101110