C # obtener información sobre la ventana activa actual
Tengo una aplicación que quiero ejecutar en segundo plano. Quiero obtener el nombre del ejecutable, por ejemplo, IExplorer.exe. He jugado un poco con el siguiente código:
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
public static void Main()
{
int chars = 256;
StringBuilder buff = new StringBuilder(chars);
while (true)
{
// Obtain the handle of the active window.
IntPtr handle = GetForegroundWindow();
// Update the controls.
if (GetWindowText(handle, buff, chars) > 0)
{
Console.WriteLine(buff.ToString());
Console.WriteLine(handle.ToString());
}
Thread.Sleep(1000);
}
}
Eso solo me da el título de la ventana, y el identificador del identificador. Quiero obtener el nombre del ejecutable (y tal vez más información).
¿Cómo logro eso?