尽管之前已经提出过这个问题,但答案中提出的代码并没有让我更进一步,所以也许有人可以对此进行阐述。
我从this回答问题获得了大部分代码。
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";
}
}
基本上我想要实现的是让这个方法返回与句柄对应的窗口的完整路径。稍后将使用编译此代码的.DLL在外部使用该方法。
我遇到的问题是,由于某些原因,资源管理器始终为NULL,因此 没有回路。
如果有人能够对此有所了解,我会很高兴,因此我和其他可能有问题的人可能知道该怎么做。
答案 0 :(得分:1)
以下是我打印每个资源管理器窗口的目录的方法:
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);
}
});
}
地址格式为file:///C:/Program%20Files/...
。
您可以将此地址格式解析为您想要的格式(删除file:///
前缀并将/
替换为\
)&amp;也解码HTML encoding。
答案 1 :(得分:0)
感谢shlatchz的回答和类似主题的各种方法,我找到了一种通过窗口句柄获取路径的方法。它或多或少是所有这些的组合,我希望它能帮助人们在未来准确地看待它。
//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;
}
返回的路径格式与shlatchz answer file:///C:/Program%20Files/...