WinAPI:
/**/
/// <summary>
/// Gets the handle of the foreground window which applies only to top-level windows (frame windows or dialog boxes).
/// </summary>
/// <returns>
/// The return value is a handle to the foreground window.
/// The foreground window can be NULL in certain circumstances, such as when a window is losing activation.
/// </returns>
[DllImport( " user32.dll " )]
public static extern IntPtr GetForegroundWindow();
/**/ /// <summary>
/// Gets the operating system process identifier of the process that maintains the a windowing system window.
/// </summary>
/// <param name="hWnd">A window handle.</param>
/// <param name="lpdwProcessId">The processID</param>
/// <returns>The operationg system process identifier of the process that the windowing system window.</returns>
[DllImport( " user32.dll " )]
public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
/// Gets the handle of the foreground window which applies only to top-level windows (frame windows or dialog boxes).
/// </summary>
/// <returns>
/// The return value is a handle to the foreground window.
/// The foreground window can be NULL in certain circumstances, such as when a window is losing activation.
/// </returns>
[DllImport( " user32.dll " )]
public static extern IntPtr GetForegroundWindow();
/**/ /// <summary>
/// Gets the operating system process identifier of the process that maintains the a windowing system window.
/// </summary>
/// <param name="hWnd">A window handle.</param>
/// <param name="lpdwProcessId">The processID</param>
/// <returns>The operationg system process identifier of the process that the windowing system window.</returns>
[DllImport( " user32.dll " )]
public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
int
pID;
IntPtr hWnd = GetForegroundWindow();
GetWindowThreadProcessId(hWnd, out pID);
Process p = Process.GetProcessById(pID);
string path = p.MainModule.FileName;
IntPtr hWnd = GetForegroundWindow();
GetWindowThreadProcessId(hWnd, out pID);
Process p = Process.GetProcessById(pID);
string path = p.MainModule.FileName;