我想知道如何使用 C# 获取当前事件窗口的路径。
我得到当前事件窗口的句柄
const int nChars = 256;
int handle = 0;
StringBuilder Buff = new StringBuilder(nChars);
handle = GetForegroundWindow();
现在如何获取此窗口的路径?
即:“我的文档”窗口的路径是
C:\Users\User\Documents
-=-=-==-=-=edit-=-=-=-=-=-
我想编写程序来监视“windows 资源管理器”并查看用户去了哪里?
(即:用户转到 c:\,然后转到程序文件,然后转到 Internet Explorer,我想获取此路径:C:\Program Files\Internet Explorer。
最佳答案
添加对“Microsoft Internet 控件”的引用 (COM)
var explorer = new SHDocVw.ShellWindowsClass().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);
}
打印 explorer.exe 实例的标题/路径,主窗口句柄在 handle
中。
关于c# - 使用窗口句柄获取当前事件窗口的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11484528/