使用窗口句柄获取当前活动窗口的路径 [英] Get path of current active window using window handle

查看:689
本文介绍了使用窗口句柄获取当前活动窗口的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何抓住用C#当前活动窗口的路径。



我得到currnet活动窗口的句柄

  const int的nChars = 256; 
INT手柄= 0;
StringBuilder的浅黄色=新的StringBuilder(nChars);

手柄= GetForegroundWindow();

现在我如何得到这个窗口的路径?



即:我的文档窗口的路径是



 Ç :\Users\User\Documents 

- = - = - = = - = - =编辑 - = - = - = - = - = - 结果
I想WIRTE计划,以监测Windows资源管理器,看看当用户进入结果
? (即:用户转至c:\,然后去程序文件,然后去Internet Explorer和我想要得到这个路径:C:\Program Files\Internet资源管理器


解决方案

添加引用(COM)为Microsoft Internet控制

  VAR探险=新SHDocVw.ShellWindowsClass()演员LT; SHDOCVW。 InternetExplorer的>()式(HWND => hwnd.HWND ==手柄)。.FirstOrDefault(); 
如果(!探险家= NULL){
串路径=新的URI(explorer.LocationURL)。 localPath来;
Console.WriteLine(NAME = {0},路径= {1},explorer.LocationName,路径);
}
点$ p>

在打印的Explorer.exe 实例的主窗口句柄的标题/路径处理。


I'd like to know how to grab the Path of the current active window using C#.

i get handle of currnet active window

        const int nChars = 256;
        int handle = 0;
        StringBuilder Buff = new StringBuilder(nChars);

        handle = GetForegroundWindow(); 

now how do i get path of this window?

i.e: Path of "my document" window is

C:\Users\User\Documents

-=-=-==-=-=edit-=-=-=-=-=-
i want to wirte program to monitor "windows explorer" and see Where the user goes?
(i.e:user go to c:\ and then go to program files and then go to Internet Explorer and i want to get this path:C:\Program Files\Internet Explorer.

解决方案

Add a reference (COM) to "Microsoft Internet Controls"

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);
}

Prints the title/path of the explorer.exe instance with the main window handle in handle.

这篇关于使用窗口句柄获取当前活动窗口的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆