这一个则比较投机,准确性不能保证,可以参考:
这个类获取当前进程的句柄:
public class MyProcess
{
private bool haveMainWindow = false;
private IntPtr mainWindowHandle = IntPtr.Zero;
private int processId = 0;
private delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
public IntPtr GetMainWindowHandle(int processId)
{
if (!this.haveMainWindow)
{
this.mainWindowHandle = IntPtr.Zero;
this.processId = processId;
EnumThreadWindowsCallback callback = new EnumThreadWindowsCallback(this.EnumWindowsCallback);
EnumWindows(callback, IntPtr.Zero);
GC.KeepAlive(callback);
this.haveMainWindow = true;
}
return this.mainWindowHandle;
}
private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
{
int num;
GetWindowThreadProcessId(new HandleRef(this, handle), out num);
if ((num == this.processId) && this.IsMainWindow(handle))
{
this.mainWindowHandle = handle;
return false;
}
return true;
}
private bool IsMainWindow(IntPtr handle)
{
return (!(GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero) && IsWindowVisible(new HandleRef(this, handle)));
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool IsWindowVisible(HandleRef hWnd);
}
下面的类获取在资源管理器中打开的窗口,并且筛选出哪些为文件夹:
public class TaskManager
{
[DllImport("User32")]
private extern static int GetWindow(int hWnd, int wCmd);
[DllImport("User32")]
private extern static int GetWindowLongA(int hWnd, int wIndx);
[DllImport("user32", CharSet = CharSet.Auto)]
private extern static int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
private const int GW_HWNDFIRST = 0;
private const int GW_HWNDNEXT = 2;
private const int GWL_STYLE = (-16);
private const int WS_VISIBLE = 268435456;
private const int WS_BORDER = 8388608;
private static List GetRunApplicationList(int handle)
{
try
{
List appString = new List();
int hwCurr;
hwCurr = GetWindow(handle, GW_HWNDFIRST);
while (hwCurr > 0)
{
int isTask = (WS_VISIBLE | WS_BORDER);
int lngStyle = GetWindowLongA(hwCurr, GWL_STYLE);
bool taskWindow = ((lngStyle & isTask) == isTask);
if (taskWindow)
{
int length = GetWindowTextLength(new IntPtr(hwCurr));
StringBuilder sb = new StringBuilder(2 * length + 1);
GetWindowText(hwCurr, sb, sb.Capacity);
string strTitle = sb.ToString();
if (!string.IsNullOrEmpty(strTitle))
{
appString.Add(strTitle);
}
}
hwCurr = GetWindow(hwCurr, GW_HWNDNEXT);
}
return appString;
}
catch (Exception ex)
{
throw new ApplicationException("读取应用程序信息时出错:" + ex.Message);
}
}
public static void PrintAppliction()
{
int pid = Process.GetCurrentProcess().Id;
IntPtr handl = new MyProcess().GetMainWindowHandle(pid);
List applictions = GetRunApplicationList(handl.ToInt32());
if (applictions != null)
{
foreach (string app in applictions)
{
if (Directory.Exists(app))
{
Console.WriteLine(app);
}
}
}
}
}