C# 擷取當前開啟的檔案夾2

來源:互聯網
上載者:User

標籤: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);                    }                }            }        }    }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.