11

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"

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
mmarques
  • 625
  • 3
  • 9
  • 27
  • Can you elaborate a bit ? I fear I don't understand how a Window can be a directory. Are you meaning to say that you get a handle to a Windows Explorer window, and you'd like to know which folder is currently opened ? – Luc Morin Jan 06 '14 at 22:24
  • 1
    Yes, i mean that: i get a handle to a Windows Explorer window, and i like to know which folder is currently opened. – mmarques Jan 06 '14 at 22:32
  • What you actually trying to do? There is likely nice way of achieving your overall goal instead of trying to find particular text element inside given window of another process and obtain its text... – Alexei Levenkov Jan 06 '14 at 22:35
  • I've changed title to be close to what it looks like you trying to achieve. Feel free to edit. Side note: before adding thank you notes please check [meta](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – Alexei Levenkov Jan 06 '14 at 22:38
  • 1
    The title is better. My main goal is to Close the window if its path follow some rules – mmarques Jan 06 '14 at 22:42
  • It's kind of rude closing windows the user has opened. What if the user was about to drop a file into that window? If this is a window you opened yourself, then you should use an Explorer Browser control. That way you control the lifetime. – Raymond Chen Jan 06 '14 at 22:48
  • 1
    Your goal is the wrong approach. If you don't want a user to have access to the folder, use folder privileges or a group policy to restrict access. Just closing UI windows that aren't yours is the absolute worst idea, IMO. – Ken White Jan 06 '14 at 22:56
  • 1
    Is it possible to create a folder and disable the ability of renaming that folder ?? – mmarques Jan 06 '14 at 22:59
  • Oh, please ask the real question right from the get go! – David Heffernan Jan 06 '14 at 23:04

3 Answers3

12

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.

Luc Morin
  • 5,302
  • 20
  • 39
0

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

Tolga Sahin
  • 319
  • 2
  • 6
-2

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?

Community
  • 1
  • 1
Bogdan_Ch
  • 3,328
  • 4
  • 23
  • 39
  • 1
    I already have the title of the window. And yes, it usually reflects the current directory opened, so what i want is the full path to that opened directory – mmarques Jan 06 '14 at 23:06
  • 4
    This is not a good idea, because it is optional for the user the let it display the fullpath via folder options. I know you say usually, but assuming something is hardly ever a good idea. – Silvermind Jan 07 '14 at 10:10
  • 2
    Sorry, had to downvote, for suggesting the crudest kludge imaginable. All the more with the Shell providing a clean, supported, and documented automation interface. – IInspectable Aug 18 '16 at 10:13
  • 1
    @Silvermind True. Its quick and easy hack, tho. The probelem comes when your path is longer than 95 characters. It gets truncated and there is no way to see it in full glory. For example, some deep VS path: `C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\Hostx64\x`. That's why I want to try to hook new explorer windows somehow and use the Shell API to get full path and force the title to display it fully or at least with `...` in the middle... 256 chars limit is already super limiting, but 95? Its ridiculous! – ScienceDiscoverer Aug 04 '22 at 04:24