如何获取前台窗口的exe路径

发布于 2024-08-21 23:38:40 字数 564 浏览 2 评论 0原文

我想获取活动前台窗口的可执行文件的路径。

我已经有了前台窗口的处理程序:

[DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();
IntPtr handlerAppActual = GetForegroundWindow();

我想获取它的可执行文件的路径,就像快捷方式一样。 (例如:C:\application\application.exe)

为什么我需要这个? 稍后使用它通过调用其进程来自动执行应用程序,如下所示:

Process process = new Process();
process.StartInfo.FileName = @parametros[0];
process.Start();

其中“parametros[0]”是文件的路径。

我正在询问前台窗口应用程序的路径,但是如果您知道任何其他方法来执行我需要的操作(获取前台应用程序的主进程以稍后执行它),我将很高兴听到它。

感谢并致敬!!!

I would like to get the executable file´s path of the active foreground window.

I already have the handler of the foreground window:

[DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();
IntPtr handlerAppActual = GetForegroundWindow();

And i would like to get the path of it´s executable file, like a shortcut. (ex: C:\application\application.exe)

Why do i need this??
To use it later to automatically execute the application with a call of its process, like this:

Process process = new Process();
process.StartInfo.FileName = @parametros[0];
process.Start();

Where "parametros[0]" is the path of the file.

I´m asking for the path of the foreground window´s application, but if you know any other way to do what i need (get the main process of the foreground application to execute it later), i would be please to hear it.

Thanks and salutes!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裸钻 2024-08-28 23:38:40

查看System.Diagnostics.Process 类。您可以使用其 MainWindowHandle 属性来请求进程的窗口句柄,并将其与您获取的窗口句柄进行比较。

要获取系统上运行的所有可用进程的列表,请使用 Process.GetProcesses ()

如果您有匹配的进程对象,请使用 Process.MainModule.FileName 属性获取可执行文件路径。

Take a look at the System.Diagnostics.Process class. You can use its MainWindowHandle property to ask for a process' window handle and compare that to the handle of the window you acquired.

To get a list of all available processes running on your system use the Process.GetProcesses ()

If you have the matching process object use the Process.MainModule.FileName property to get the executable file path.

顾挽 2024-08-28 23:38:40

您可以使用 GetWindowThreadProcessId 获取进程 ID ,使用 OpenProcess 获取进程句柄进程 ID,然后使用 psapi 方法 GetProcessImageFileName 在句柄上获取可执行文件的路径。

或者(基于 Frank 的答案),一旦获得了 Process Id,就可以使用 Process.GetProcessById(pid) ,然后使用 的 MainModule.FileName 属性处理对象实例。这样,您只需要 p/调用 GetWindowThreadProcessId,甚至不需要使用 OpenProcess/GetProcessImageFileName。

You can use GetWindowThreadProcessId to get the process Id, use OpenProcess to get a process handle from the process Id and then the use the psapi method GetProcessImageFileName on the handle to get the path to the executable.

Or (based on Frank's answer), once you have the Process Id, you can use Process.GetProcessById(pid) and then use MainModule.FileName property of the Process object instance. This way you only need to p/invoke GetWindowThreadProcessId and not even use OpenProcess/GetProcessImageFileName.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文