Reputation: 11
Even though this question has already been asked before, the code proposed in the answers didn't get me much further so maybe someone can shed light on this.
I got most of the code from this answered question.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace TestClassLibrary
{
public class ClassTest
{
public ClassTest()
{
}
public static void GetWindowPath(int handle, out string paths) {
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
var explorer = shellWindows.Cast<SHDocVw.InternetExplorer>().Where(hwnd => hwnd.HWND == handle).FirstOrDefault();
if (explorer != null)
{
string path = new Uri(explorer.LocationURL).LocalPath;
Console.WriteLine("name={0}, path={1}", explorer.LocationName, path);
paths = path;
}
else
{
//Console.WriteLine("name={0}, path={1}", explorer.LocationName, path);
paths = "Test";
}
}
Basically what I want to achieve is to have this method return the full path of the window that corresponds to the handle. The method will later on be used externally using the .DLL that this code is compiled into.
The problem I have is that for some reason explorer is always NULL and thus doesn't return a path.
I'd be happy if anyone could shed some light on this, so that I and probably other people who have problems on that matter may know what to do here.
Upvotes: 1
Views: 901
Reputation: 11
Thanks to shlatchz answer and various approaches from similar topics, I found a way to obtain the path through the handle of the window. It's more or less a mix of all of them and I hope it will help people looking exactly for this in the future.
//Method to obtain the window path that corresponds to the handle passed
public static string GetWindowPath(int handle)
{
//Default value for the variable returned at the end of the method if no path was found
String Path = "";
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
foreach (SHDocVw.InternetExplorer window in shellWindows)
{
//If Window handle corresponds to passed handle
if (window.HWND == handle)
{
//Print path of the respective folder and save it within the returned variable
Console.WriteLine(window.LocationURL);
Path = window.LocationURL;
break;
}
}
//If a path was found, retun the path, otherwise return ""
return Path;
}
The path returned will be in the same format as provided in shlatchz answer file:///C:/Program%20Files/...
Upvotes: 0
Reputation: 1652
Here is how I print the directory of each explorer window:
public void RefreshWindow()
{
Guid CLSID_ShellApplication = new Guid("13709620-C279-11CE-A49E-444553540000");
Type shellApplicationType = Type.GetTypeFromCLSID(CLSID_ShellApplication, true);
object shellApplication = Activator.CreateInstance(shellApplicationType);
object windows = shellApplicationType.InvokeMember("Windows", System.Reflection.BindingFlags.InvokeMethod, null, shellApplication, new object[] { });
Type windowsType = windows.GetType();
object count = windowsType.InvokeMember("Count", System.Reflection.BindingFlags.GetProperty, null, windows, null);
Parallel.For(0, (int)count, i =>
{
object item = windowsType.InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, null, windows, new object[] { i });
Type itemType = item.GetType();
string itemName = (string)itemType.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, item, null);
if (itemName == "Windows Explorer" || itemName == "File Explorer")
{
string currDirectory = (string)itemType.InvokeMember("LocationURL", System.Reflection.BindingFlags.GetProperty, null, item, null);
Console.WriteLine(currDirectory);
}
});
}
The address format is file:///C:/Program%20Files/...
.
You can parse this address format to the one you want (By removing the file:///
prefix and replacing /
with \
) & also decoding the HTML encoding.
Upvotes: 1