繁体   English   中英

如何使用c#查找带有鼠标指针的最近打开的/当前活动的文件的路径

[英]How to Find the path of an recently opened/ Currently Active file with a Mouse pointer in it using c#

我想为当前打开的文件找到pah,该文件在Windows资源管理器中当前处于活动状态。 在单击文件夹时,我已经找到了文件夹的位置,但是我们如何获取文件的路径,例如文本文件或单词或类似的东西

private string GetActiveWindowPath()
        {
            const int nChars = 256;
            StringBuilder Buff = new StringBuilder(nChars);
            IntPtr handle = GetForegroundWindow();
            int handleint = int.Parse(handle + "");
            SHDocVw.ShellWindows explorer = new SHDocVw.ShellWindows();

            //var xy = new SHDocVw.InternetExplorerMedium();

            var xpl = explorer.Cast<SHDocVw.InternetExplorerMedium>().Where(hwnd => hwnd.HWND == handleint).FirstOrDefault();
            if (xpl != null)
            {
                // this will get the folder location
                string path = new Uri(xpl.LocationURL).LocalPath;
                return ("location:" + xpl.LocationName + " path:" + path );
            }
            return "HWND" + handleint ;
        } 

在此处输入图片说明

在此处输入图片说明

我可以像看到的那样获取打开的窗口的路径,而不是文本文件的路径

 private string GetMainModuleFilepath(int processId)
    {
        string wmiQueryString = "SELECT * FROM Win32_Process WHERE ProcessId = " + processId;
        using (var searcher = new ManagementObjectSearcher(wmiQueryString))
        {
            using (var results = searcher.Get())
            {
                ManagementObject mo = results.Cast<ManagementObject>().FirstOrDefault();
                if (mo != null)
                {
                    return (string)mo["CommandLine"];
                }
            }
        }
        Process testProcess = Process.GetProcessById(processId);
        return null;
    }

之后,您将获得可执行文件名,其文件名如下

之后,根据需要拆分“ c:.. \\ notepade.exe” D:\\ New Folder \\ sampleFile.txt”,即可获取路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM