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"
If you're using Windows 10, hold down Shift on your keyboard and right-click on the file, folder, or library for which you want a link. If you're using Windows 11, simply right-click on it. Then, select “Copy as path” in the contextual menu.
A path is a slash-separated list of directory names followed by either a directory name or a file name. A directory is the same as a folder.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With