我怎样才能在前台窗口的exe文件路径 [英] How can i get the exe path of the foreground window

查看:159
本文介绍了我怎样才能在前台窗口的exe文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得可执行file's活动前台窗口的路径

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

和我想获得的汇入作业路径的可执行文件,就像一个快捷方式。 (例如:C:\application\application.exe)

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



在哪里parametros [0]是文件的路径。

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

I'm询问前台window's应用程序的路径,但如果你知道任何其他方式做我需要(让前台应用程序的主要过程以后执行它)我会请听听吧。

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.

感谢和致敬!!!

推荐答案

您可以使用 GetWindowThreadProcessId 以得到进程ID,请使用调用OpenProcess 得到从进程ID的进程句柄,然后使用PSAPI方法的 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.

或(根据弗兰克的答案),一旦你有进程ID,您可以使用 Process.GetProcessById(PID),然后用 MainModule.FileName 财产过程对象实例。这样,你只需要的P / Invoke GetWindowThreadProcessId 键,甚至不使用调用OpenProcess / GetProcessImageFileName。

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.

这篇关于我怎样才能在前台窗口的exe文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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