[英]How can i get the exe path of the foreground window
我想獲取活動前台窗口的可執行文件路徑。
我已經擁有前台窗口的處理程序:
[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]”是文件的路徑。
我要求前景窗口的應用程序的路徑,但如果你知道任何其他方式來做我需要的(獲得前台應用程序的主要過程以后執行它),我會很高興聽到它。
謝謝,致敬!
看一下System.Diagnostics.Process類。 您可以使用其MainWindowHandle
屬性來請求進程的窗口句柄,並將其與您獲取的窗口的句柄進行比較。
要獲取系統上運行的所有可用進程的列表,請使用Process.GetProcesses ()
如果您具有匹配的流程對象,請使用Process.MainModule.FileName屬性來獲取可執行文件路徑。
您可以使用GetWindowThreadProcessId來獲取進程Id,使用OpenProcess從進程Id獲取進程句柄,然后在句柄上使用psapi方法GetProcessImageFileName來獲取可執行文件的路徑。
或者(根據Frank的回答),一旦擁有Process Id,就可以使用Process.GetProcessById(pid)
,然后使用Process
對象實例的MainModule.FileName
屬性。 這樣你只需要調用GetWindowThreadProcessId
,甚至不需要使用OpenProcess / GetProcessImageFileName。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.