从Explorer窗口获取文件夹路径

mma*_*ues 7 .net c# winapi

我有一个指向打开的资源管理器窗口的指针,我想知道它的完整路径.

例如:

int hWnd = FindWindow(null, "Directory");
Run Code Online (Sandbox Code Playgroud)

但是现在,如何获取目录的完整路径就好了 "C:\Users\mm\Documents\Directory"

Luc*_*rin 9

以下是获取该信息的方法:

    IntPtr MyHwnd = FindWindow(null, "Directory");
    var t = Type.GetTypeFromProgID("Shell.Application");
    dynamic o = Activator.CreateInstance(t);
    try
    {
        var ws = o.Windows();
        for (int i = 0; i < ws.Count; i++)
        {
            var ie = ws.Item(i);
            if (ie == null || ie.hwnd != (long)MyHwnd) continue;
            var path = System.IO.Path.GetFileName((string)ie.FullName);
            if (path.ToLower() == "explorer.exe")
            {
                var explorepath = ie.document.focuseditem.path;
            }
        }
    }
    finally
    {
        Marshal.FinalReleaseComObject(o);
    } 
Run Code Online (Sandbox Code Playgroud)

改编自:http://msdn.microsoft.com/en-us/library/windows/desktop/bb773974(v = vs.85).aspx

干杯

编辑:我将没有返回完整路径的ie.locationname更改为ie.document.focuseditem.路径,似乎到目前为止工作.

  • 我发现 `var explorepath = new Uri(ie.LocationURL).LocalPath;` 在没有选定项目时也可以工作。 (3认同)
  • 是的,我明白:)我刚刚提出了建设性意见!你的代码真的很有帮助。多谢 (2认同)