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

 
 
因demo,故没有完整的异常验证,转载请注明出处~win7下测试通过,xp有点不一样,具体请用spy++查看
 
 class Program
    {
        
        public delegate bool CallBack(int hwnd, int y);
 
        //该函数枚举所有屏幕上的顶层窗口,并将窗口句柄传送给应用程序定义的回调函数。
        //回调函数返回FALSE将停止枚举,否则EnumWindows函数继续到所有顶层窗口枚举完为止。
        [DllImport("user32.dll")]
        public static extern int EnumWindows(CallBack x, int y);
 
        //该函数将指定窗口的标题条文本(如果存在)拷贝到一个缓存区内
        [DllImport("user32.dll")]
        public static extern int GetWindowText(int hwnd, StringBuilder lptrString, int nMaxCount);
 
        //该函数获得一个指定子窗口的父窗口句柄
        [DllImport("user32.dll")]
        public static extern int GetParent(int hwnd);
 
        //该函数获得给定窗口的可视状态。
        [DllImport("user32.dll")]
        public static extern bool IsWindowVisible(int hwnd);
 
        //获取窗体类名
        [DllImport("User32.Dll ")]
        public static extern void GetClassName(IntPtr hwnd, StringBuilder s, int nMaxCount);
 
        //获取窗体的子窗体句柄
        [DllImport("User32.dll ")]
        public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText);
 
        ///
        /// 根据句柄获取类名
        ///
        ///
        ///
        private string GetFormClassName(IntPtr ptr)
        {
            StringBuilder nameBiulder = new StringBuilder(255);
            GetClassName(ptr, nameBiulder, 255);
            return nameBiulder.ToString();
        }
 
        ///
        /// 根据句柄获取窗口标题
        ///
        ///
        ///
        private string GetFormTitle(IntPtr ptr)
        {
            StringBuilder titleBiulder = new StringBuilder(255);
            GetWindowText((int)ptr, titleBiulder, 255);
            return titleBiulder.ToString();
        }
 
        public bool Report(int hwnd, int lParam)
        {
            int pHwnd = GetParent(hwnd);
            //如果再没有父窗口并且为可视状态的窗口,则遍历
            if (pHwnd == 0 && IsWindowVisible(hwnd) == true)
            {
                IntPtr cabinetWClassIntPtr = new IntPtr(hwnd);
                string cabinetWClassName = GetFormClassName(cabinetWClassIntPtr);
                //如果类名为CabinetWClass ,则为explorer窗口,可以通过spy++查看窗口类型
                if (cabinetWClassName.Equals("CabinetWClass", StringComparison.OrdinalIgnoreCase))
                {
                    //下面为一层层往下查找,直到找到资源管理器的地址窗体,通过他获取窗体地址
                    IntPtr workerWIntPtr = FindWindowEx(cabinetWClassIntPtr, IntPtr.Zero, "WorkerW", null);
                    IntPtr reBarWindow32IntPtr = FindWindowEx(workerWIntPtr, IntPtr.Zero, "ReBarWindow32", null);
                    IntPtr addressBandRootIntPtr = FindWindowEx(reBarWindow32IntPtr, IntPtr.Zero, "Address Band Root", null);
                    IntPtr msctls_progress32IntPtr = FindWindowEx(addressBandRootIntPtr, IntPtr.Zero, "msctls_progress32", null);
                    IntPtr breadcrumbParentIntPtr = FindWindowEx(msctls_progress32IntPtr, IntPtr.Zero, "Breadcrumb Parent", null);
                    IntPtr toolbarWindow32IntPtr = FindWindowEx(breadcrumbParentIntPtr, IntPtr.Zero, "ToolbarWindow32", null);
 
 
                    string title = GetFormTitle(toolbarWindow32IntPtr);
                    Console.WriteLine(title);
 
                    int index = title.IndexOf(':');
                    index++;
                    string path = title.Substring(index, title.Length - index);
                    Console.WriteLine(path);
                }
            }
            return true;
        }
 
 
        static void Main(string[] args)
        {
            EnumWindows(new Program().Report, 0);
 
            Console.ReadKey();
        }
    }

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

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

    这一个则比较投机,准确性不能保证,可以参考:   这个类获取当前进程的句柄:  public class MyProcess     {         private bool haveMainWi ...

  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. go语言基础之go猜数字游戏

    1. 产生一个随机的4位数 示例1: package main import "fmt" import "math/rand" import "tim ...

  2. 理解 CSS 的 z-index 属性

    通常认为HTML页面是二维的,但实际上,CSS还有一个z-index属性,允许层叠元素. 所有的盒模型元素都处于三维坐标系中. 除了我们常用的横坐标和纵坐标, 盒模型元素还可以沿着“z轴”层叠摆放, ...

  3. 提高你开发效率的十五个 Visual Studio 使用技巧

    相信做开发的没有不重视效率的.开发C#,VB的都知道,我们很依赖VS,或者说,我们很感谢VS.能够对一个IDE产生依赖,说明这个IDE确实 有它的独特之处.无容置疑,VS是一个非常强大的IDE,它支持 ...

  4. android 百度地图demo 随感

    最近项目组的老大要我对百度的android的sdk进行一段的预研,由于技术太菜,出了不少的错误,因此有一点感悟了. 嗨,这个错误浪费了我一天的时间的时候,我按照百度的技术文档一步步的来做,每部基本上都 ...

  5. [Big Data] Week4B (Basic)

    Question 1 Note: In this question, all columns will be written in their transposed form, as rows, to ...

  6. 整数划分问题--DFS

    单点时限:1000ms 内存限制:256MB 描写叙述 Given two positive integers N and M, please divide N into several intege ...

  7. cocos lua 加密与解密 混淆 (版本号cocos3.4)

    cocos luacompile cocos luacompile Overview Usage Available Arguments Samples Overview Compile the .l ...

  8. Simple example

    This is a simple example showing a small window. Yet we can do a lot with this window. We can resize ...

  9. tomcat启用压缩的方式

    <Connector port="7070" protocol="HTTP/1.1"connectionTimeout="20000" ...

  10. 解决 在POM配置Maven plugin提示错误“Plugin execution not covered by lifecycle configuration”

    eclipse在其POM文件的一处提示出错如下: Plugin execution not covered by lifecycle configuration: org.apache.maven.p ...