【问题标题】:How can i get the exe path of the foreground window如何获取前台窗口的exe路径
【发布时间】:2011-01-16 23:06:48
【问题描述】:

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

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

[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]”是文件的路径。

我正在询问前台窗口应用程序的路径,但如果您知道任何其他方式来做我需要的事情(让前台应用程序的主进程稍后执行它),我会请听听。

谢谢和致敬!!!

【问题讨论】:

    标签: c# window foreground


    【解决方案1】:

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

    或者(基于弗兰克的回答),一旦你有进程 ID,你可以使用 Process.GetProcessById(pid) 然后使用 Process 对象实例的 MainModule.FileName 属性。这样你只需要 p/invoke GetWindowThreadProcessId 甚至不需要使用 OpenProcess/GetProcessImageFileName。

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • MainWindowHandle 不必与 GetForeGroundWindow 返回的句柄相同,即使是同一个进程。
      猜你喜欢
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 2017-08-12
      • 1970-01-01
      相关资源
      最近更新 更多