gpt4 book ai didi

c# - 使用窗口句柄获取当前事件窗口的路径

转载 作者:太空狗 更新时间:2023-10-30 00:54:50 36 4
gpt4 key购买 nike

我想知道如何使用 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。 enter image description here

最佳答案

添加对“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/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com