如何获取前台窗口的exe路径
我想获取活动前台窗口的可执行文件的路径。
我已经有了前台窗口的处理程序:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看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.
您可以使用 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 useMainModule.FileName
property of theProcess
object instance. This way you only need to p/invokeGetWindowThreadProcessId
and not even use OpenProcess/GetProcessImageFileName.