使用窗口句柄获取当前活动窗口的路径

Ami*_*inM 4 c# winapi

我想知道如何使用C#获取当前活动窗口的路径.

我得到currnet活动窗口的句柄

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

        handle = GetForegroundWindow(); 
Run Code Online (Sandbox Code Playgroud)

现在我怎么得到这个窗口的路径?

ie:"我的文档"窗口的路径是

C:\Users\User\Documents
Run Code Online (Sandbox Code Playgroud)

- = - = - == - = - =编辑 - = - = - = - = - = -
我想用程序监控"Windows资源管理器"并查看用户去哪里?
(即:用户转到c:\然后转到程序文件,然后转到Internet Explorer,我想获得此路径:C:\ Program Files\Internet Explorer. 在此输入图像描述

Ale*_* K. 7

添加引用(COM)到"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);
}
Run Code Online (Sandbox Code Playgroud)

使用主窗口句柄打印explorer.exe实例的标题/路径handle.