mmarques
mmarques

Reputation: 655

Get folder path from Explorer window

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

For Example:

int hWnd = FindWindow(null, "Directory");

But now, how to obtain the directory full path like "C:\Users\mm\Documents\Directory"

Upvotes: 14

Views: 7951

Answers (3)

Tolga Sahin
Tolga Sahin

Reputation: 347

Here the list of System.__ComObject's properties.

PS C:\NodeJs\loginapp> $Browser | gm

TypeName: System.__ComObject#{d30c1661-cdaf-11d0-8a3e-00c04fc9e26e}

Name                 MemberType Definition
----                 ---------- ----------
ClientToWindow       Method     void ClientToWindow (int, int)
ExecWB               Method     void ExecWB (OLECMDID, OLECMDEXECOPT, Variant, Variant)
GetProperty          Method     Variant GetProperty (string)
GoBack               Method     void GoBack ()
GoForward            Method     void GoForward ()
GoHome               Method     void GoHome ()
GoSearch             Method     void GoSearch ()
Navigate             Method     void Navigate (string, Variant, Variant, Variant, Variant)
Navigate2            Method     void Navigate2 (Variant, Variant, Variant, Variant, Variant)
PutProperty          Method     void PutProperty (string, Variant)
QueryStatusWB        Method     OLECMDF QueryStatusWB (OLECMDID)
Quit                 Method     void Quit ()
Refresh              Method     void Refresh ()
Refresh2             Method     void Refresh2 (Variant)
ShowBrowserBar       Method     void ShowBrowserBar (Variant, Variant, Variant)
Stop                 Method     void Stop ()
AddressBar           Property   bool AddressBar () {get} {set}
Application          Property   IDispatch Application () {get}
Busy                 Property   bool Busy () {get}
Container            Property   IDispatch Container () {get}
Document             Property   IDispatch Document () {get}
FullName             Property   string FullName () {get}
FullScreen           Property   bool FullScreen () {get} {set}
Height               Property   int Height () {get} {set}
HWND                 Property   int HWND () {get}
Left                 Property   int Left () {get} {set}
LocationName         Property   string LocationName () {get}
LocationURL          Property   string LocationURL () {get}
MenuBar              Property   bool MenuBar () {get} {set}
Name                 Property   string Name () {get}
Offline              Property   bool Offline () {get} {set}
Parent               Property   IDispatch Parent () {get}
Path                 Property   string Path () {get}
ReadyState           Property   tagREADYSTATE ReadyState () {get}
RegisterAsBrowser    Property   bool RegisterAsBrowser () {get} {set}
RegisterAsDropTarget Property   bool RegisterAsDropTarget () {get} {set}
Resizable            Property   bool Resizable () {get} {set}
Silent               Property   bool Silent () {get} {set}
StatusBar            Property   bool StatusBar () {get} {set}
StatusText           Property   string StatusText ($) {get} {set}
TheaterMode          Property   bool TheaterMode () {get} {set}
ToolBar              Property   int ToolBar () {get} {set}
Top                  Property   int Top () {get} {set}
TopLevelContainer    Property   bool TopLevelContainer () {get}
Type                 Property   string Type () {get}
Visible              Property   bool Visible () {get} {set}
Width                Property   int Width () {get} {set}

i changed the code above like this

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) continue;
                var path = System.IO.Path.GetFileName((string)ie.FullName);
                if (path.ToLower() == "explorer.exe")
                {
                    string locationPath = ie.LocationURL;
                }
            }
        }
        finally
        {
            Marshal.FinalReleaseComObject(o);
        }

ie.LocationURL returns path of opened window

Look

Upvotes: 0

Luc Morin
Luc Morin

Reputation: 5380

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.

Upvotes: 13

Bogdan_Ch
Bogdan_Ch

Reputation: 3336

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?

Upvotes: -2

Related Questions