標籤:style os io 檔案 for ar cti div
這一個則比較投機,準確性不能保證,可以參考:
這個類擷取當前進程的控制代碼: 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); } } } } }