C# 获取所有资源管理器窗口句柄和对应路径
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices;
public class GetExplorerHandle { [DllImport("user32.dll")] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern int GetWindowTextLengthW(IntPtr hWnd);
public static List<IntPtr> GetExplorerHandleList()
{
IntPtr mainWindow = FindWindow("CabinetWClass", null);
List<IntPtr> list = new List<IntPtr>();
if (mainWindow != IntPtr.Zero)
{
list.Add(mainWindow);
IntPtr childWindow = FindWindowEx(mainWindow, IntPtr.Zero, "CabinetWClass", null);
while (childWindow != IntPtr.Zero)
{
list.Add(childWindow);
childWindow = FindWindowEx(mainWindow, childWindow, "CabinetWClass", null);
}
}
return list;
}
public static List<string> GetExplorerPathList()
{
List<string> pathList = new List<string>();
List<IntPtr> handleList = GetExplorerHandleList();
foreach (IntPtr handle in handleList)
{
int length = GetWindowTextLengthW(handle);
if (length > 0)
{
StringBuilder sb = new StringBuilder(length + 1);
GetWindowTextW(handle, sb, sb.Capacity);
pathList.Add(sb.ToString());
}
}
return pathList;
}
}
原文地址: https://www.cveoy.top/t/topic/lfLy 著作权归作者所有。请勿转载和采集!