我用C#编写一个程序来获取任务栏中的文件夹(也就是说,这个程序能够知道电脑上哪些文件夹被打开),而且可以得这些文件夹的路径,当有新的文件夹打开时马上获取这个新打开的文件夹的路径,也就是能实时监控。比如说windows任务管理器的“应用程序”中就可以看到哪些文件夹被打开了。请问C#能实现这样的功能吗?
    

解决方案 »

  1.   

    WMI你可以看看和这个!也许对你有帮助哦!应该对你有帮助
      

  2.   

    FileSystemWatcher对象可以监控C#目录发生的变化
      

  3.   

    FileSystemWatcher是监控制定文件夹,而我要的是要获得那些被打开的文件夹的列表,求教
      

  4.   


       private void Form2_Load(object sender, EventArgs e)
            {
                EnumWindowsProc _EunmWindows = new EnumWindowsProc(NetEnumWindows);
                EnumWindows(_EunmWindows, 0);            for (int i = 0; i != m_ViewDirectoryPath.Count; i++)
                {
                    MessageBox.Show(m_ViewDirectoryPath[i]);
                } 
              
            }        [DllImport("User32.dll", CharSet = CharSet.Auto)]
            public static extern int GetClassName(IntPtr hWnd, out STRINGBUFFER ClassName, int nMaxCount);        [DllImport("user32.dll")]
            public static extern bool IsWindowVisible(IntPtr hWnd);        [DllImport("user32.dll")]
            public static extern int EnumChildWindows(IntPtr hWndParent, EnumWindowsProc ewp, int lParam);        [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam);        [DllImport("user32.dll")]
            public static extern int EnumWindows(EnumWindowsProc ewp, int lParam);        public delegate bool EnumWindowsProc(IntPtr p_Handle, int p_Param);
            private List<string> m_ViewDirectoryPath = new List<string>();        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
            public struct STRINGBUFFER
            {
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 512)]
                public string szText;
            }        private bool NetEnumWindows(IntPtr p_Handle, int p_Param)
            {
                if (!IsWindowVisible(p_Handle)) return true;            STRINGBUFFER _ClassName = new STRINGBUFFER();
                GetClassName(p_Handle, out _ClassName, 255);            if(_ClassName.szText.Trim()=="CabinetWClass")
                {
                    EnumWindowsProc _EunmControl = new EnumWindowsProc(NetEnumControl);
                    EnumChildWindows(p_Handle, _EunmControl, 0);    
                }
                return true;
            }       
            private bool NetEnumControl(IntPtr p_Handle, int p_Param)
            {
                STRINGBUFFER _ClassName = new STRINGBUFFER();
                GetClassName(p_Handle, out _ClassName, 255);
                if (_ClassName.szText == "Edit")
                {
                    StringBuilder _TextStringA = new StringBuilder(1024);
                    SendMessage(p_Handle, 0x000D, 1024, _TextStringA);
                    m_ViewDirectoryPath.Add(_TextStringA.ToString());
                }
                return true;
            }
    这个意思?
      

  5.   


    等我看看你的代码,另外
    WMI是做什么的啊?
      

  6.   

    FileSystemWatcher监控某个文件时,当该文件夹中有一文件夹也被FileSystemWatcher监控的话,我们会发现这个文件夹是不能删除的,提示是文件夹在被使用,所以郁闷,就改成用获取监控任务栏被打开的文件夹,我觉得FileSystemWatcher不妥,不知道对不对,还是FileSystemWatcher有更高明的应用吗?