How can i get foreground exe path

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

I already have a foreground window handler:

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

And I would like to get the path to its executable, for example, a shortcut. (for example: C: \ application \ application.exe)

Why do I need it? To use it later to automatically run the application with a call to its process, for example:

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

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

I ask you to indicate the path to the application of the foreground window, but if you know any other way to do what I need (get the main foreground process for its implementation later), I would be glad to listen to it.

Thanks and welcome !!!

+3
2

GetWindowThreadProcessId, , OpenProcess, , psapi GetProcessImageFileName , .

( ), , Process.GetProcessById(pid), MainModule.FileName Process. p/invoke GetWindowThreadProcessId OpenProcess/GetProcessImageFileName.

0

System.Diagnostics.Process. MainWindowHandle .

, , Process.GetProcesses ()

, Process.MainModule.FileName, .

+1

Source: https://habr.com/ru/post/1732841/


All Articles