这一个则比较投机,准确性不能保证,可以参考:

 
这个类获取当前进程的句柄:
 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);
                    }
                }
            }
        }
    }
 
 

C# 获取当前打开的文件夹2的更多相关文章

  1. C# 获取当前打开的文件夹

    最近做一个项目,有一个功能点需要获取当前打开的文件夹,网上查资料+自己摸索,整理出如下代码,鉴于网上完整的代码比较少,顾贴出来,以供参考.如有更好的建议,欢迎留言.     因demo,故没有完整的异 ...

  2. C#项目打开/保存文件夹/指定类型文件,获取路径

    C#项目打开/保存文件夹/指定类型文件,获取路径 转:http://q1q2q363.xiaoxiang.blog.163.com/blog/static/1106963682011722424325 ...

  3. [转]C#中调用资源管理器(Explorer.exe)打开指定文件夹 + 并选中指定文件 + 调用(系统默认的播放类)软件(如WMP)打开(播放歌曲等)文件

    原文:http://www.crifan.com/csharp_call_explorer_to_open_destinate_folder_and_select_specific_file/ C#中 ...

  4. 怎样使用windows命令行,用notepad打开某文件夹下面的所有文件

    http://zhidao.baidu.com/question/2138815012359999388.html __________________________________________ ...

  5. MFC中打开选择文件夹对话框,并将选中的文件夹地址显示在编辑框中

    一般用于选择你要将文件保存到那个目录下,此程序还包含新建文件夹功能 BROWSEINFO bi; ZeroMemory(&bi, sizeof(BROWSEINFO));  //指定存放文件的 ...

  6. windows资源管理器多标签打开 windows文件夹多标签浏览 浏览器tab页面一样浏览文件夹 clover win8 win10 报错 无响应问题怎么解决 clover卡死 clover怎么换皮肤

    大家都知道,我们打开一堆文件夹的时候,是什么样子 “厚厚的一叠”图标堆叠在一起的,非常的不方便 那么,是不是可以像浏览器一样的tab页面展示呢? 答案是可以的 安装好就是这样子的 是不是方便漂亮了很多 ...

  7. Jupyter Notebook 修改默认打开的文件夹的位置

    初次使用Jupyter Notebook,确实好用啊!!,又好看又好用,不过还是遇到了一个问题,安装好之后,打开Jupyter Notebook 的时候,默认的文件夹的位置是C盘下面的XXX目录,但是 ...

  8. 获取AFP共享的文件夹及其权限

    获取AFP共享的文件夹及其权限   获取AFP服务的认证信息后,渗透测试人员就可以使用afp-showmount脚本获取共享的文件夹信息,以及各级用户权限信息.其中,用户包括所有者.组.Everyon ...

  9. JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了。

    JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了.因为tomc ...

随机推荐

  1. iOS:UIToolBar控件的使用

    UIToolBar控件:是经常使用的一个工具条控件,虽然在上面可以添加子控件,但是toolbar中只能添加UIBarButtonItem类型的子控件,其他子控件会被包装成这种类型的,例如UIButto ...

  2. Java:IO流的综合用法(从键盘录入数据并打印在控制台上)

    import java.io.*; public class IOTestDouble { public static void main(String[] args)throws Exception ...

  3. input框设置onInput事件只能输入数字,能兼容火狐IE9

    使用onInput()事件 onInput()是 HTML5 的标准事件,对于检测 textarea, input:text, input:password 和 input:search 这几个元素通 ...

  4. IOS之UITabBarController

    在学习IOS开发过程中,针对于UITabBarController的使用也不少出现,UITabBarController和UINavigationController类似,UITabBarContro ...

  5. 网络结构设计——负载均衡之LVS学习笔记(二)

    LVS按个人理解的说就是将一台Linux服务器当作路由器等功能的技术.LVS---Linux虚拟服务器. LVS实现了三种IP负载均衡技术VS/NAT.VS/TUN.VS/DR. 今天简单分享一下我在 ...

  6. Lessons learned developing a practical large scale machine learning system

    原文:http://googleresearch.blogspot.jp/2010/04/lessons-learned-developing-practical.html Lessons learn ...

  7. Mac下Sublime Text 总是以新窗口打开文件的解决办法

    Mac下的Sublime有个毛病,经常打开后,之前打开的窗口都没了,太难受了. Windows/Linux下的sublime总是默认的以标签页的形式打开关联的文件,但是在Mac下使用Sublime打开 ...

  8. Untracked Files Prevent Checkout move or commit them before checkout

    点开View Files... 查看里面的文件名称,在项目的.idea文件夹中删掉ViewFiles显示的文件夹名称就好

  9. ZH奶酪:putty远程登录Linux服务器非常慢

    11.pytty远程登录Linux服务器非常慢 http://www.it165.net/os/html/201209/3425.html 12.启动SSHD服务报错 http://blog.chin ...

  10. Windows 之 防火墙

          对于只使用浏览.电子邮件等系统自带的网络应用程序,Windows防火墙(firewall)根本不会产生影响.也就是说,用IE.OutlookExpress等系统自带的程序进行网络连接,防火 ...