3

I would like to get the Path of the windows which has the focus.

Ex: I have 3 windows Opened
a. C:\Windows
b. C:\Windows\System32
c. C:\Users\COMP-0\Documents

And i am working on c (C:\Users\COMP-0\Documents)

So i would like to get this path (C:\Users\COMP-0\Documents) programmatically in C#.

cool buzz
  • 129
  • 2
  • 6
  • Possible duplicate of http://stackoverflow.com/questions/8292953/get-current-selection-in-windowsexplorer-from-a-c-sharp-application or http://stackoverflow.com/questions/3382946/get-selected-items-of-folder-with-winapi/ – John Dec 21 '14 at 14:10

1 Answers1

6

Expanding on this answer to get the selected files in a folder, you can use a similar approach to get the current folder and therefore it's path.

This needs some COM and requires:

There are a couple of caveats to be aware of:

  • Special folders (Favourites, My Computer etc) will give you the file path as "::{GUID}" where the GUID points to the CLSID for that folder in the registry. It is probably possible to convert that value to a path.
  • Going to "Desktop" will return null for the current folder
  • Focussing Internet Explorer will trigger a match on the active window so we need to ensure we are in a Shell Folder

If in a special folder or Desktop this code will just return the current window title - usually the name of the special folder - using the details in this answer.

private static string GetActiveExplorerPath()
{
    // get the active window
    IntPtr handle = GetForegroundWindow();

    // Required ref: SHDocVw (Microsoft Internet Controls COM Object) - C:\Windows\system32\ShDocVw.dll
    ShellWindows shellWindows = new SHDocVw.ShellWindows();

    // loop through all windows
    foreach (InternetExplorer window in shellWindows)
    {
        // match active window
        if (window.HWND == (int)handle)
        {
            // Required ref: Shell32 - C:\Windows\system32\Shell32.dll
            var shellWindow = window.Document as Shell32.IShellFolderViewDual2;

            // will be null if you are in Internet Explorer for example
            if (shellWindow != null)
            {
                // Item without an index returns the current object
                var currentFolder = shellWindow.Folder.Items().Item();

                // special folder - use window title
                // for some reason on "Desktop" gives null
                if (currentFolder == null || currentFolder.Path.StartsWith("::"))
                {
                    // Get window title instead
                    const int nChars = 256;
                    StringBuilder Buff = new StringBuilder(nChars);
                    if (GetWindowText(handle, Buff, nChars) > 0)
                    {
                        return Buff.ToString();
                    }
                }
                else
                {
                    return currentFolder.Path;
                }
            }

            break;
        }
    }

    return null;
}

// COM Imports

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
Community
  • 1
  • 1
Rhumborl
  • 16,349
  • 4
  • 39
  • 45