Get folder path from Explorer window

前端 未结 2 1813
攒了一身酷
攒了一身酷 2020-12-06 07:02

I have a pointer to an opened Explorer Window and i want to know its full path.

For Example:

int hWnd = FindWindow(null, \"Directory\");
相关标签:
2条回答
  • 2020-12-06 07:17

    If you run spy++ and target Expolrer window you will see that Title or Caption of that window usually reflects the current directory opened by user

    So what you need is that using window handle you need to get its caption. I would suggest following links that will guide you

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/fd03235e-22af-41a4-aa95-2806b3cb1114/win32-getting-a-window-title-from-a-hwnd?forum=csharpgeneral

    How to get the name of an External window in C# Application?

    0 讨论(0)
  • 2020-12-06 07:35

    Here's a way to obtain that information:

        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);
        } 
    

    Adapted from this: http://msdn.microsoft.com/en-us/library/windows/desktop/bb773974(v=vs.85).aspx

    Cheers

    EDIT: I changed ie.locationname, which was not returning the full path, to ie.document.focuseditem. path, which seems to be working so far.

    0 讨论(0)
提交回复
热议问题