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

标签 c# winapi

我想知道如何使用 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/

相关文章:

c# - ASP.Net 如何在提交时删除/忽略特定元素的验证?

C# - 如何替换重音字符,即 "-É"为 "- É"

使用 Windows API 比较线程 ID

多次调用同一个 threadproc(),第一次调用没有完成

c# - 如何实现 Azure 表存储以允许单元测试

c# - 使用 AssemblyBuilder 构建复杂类型

c# - 如何在没有项目文件的情况下运行 C# 项目?

c++ - 在 DirectX 11 中更改分辨率

c - 跟踪工具提示导致重绘速度极其缓慢的灰色 "trail"

c++ - win32api : incorporate bitmaps in text lines