C # obtém informações sobre a janela ativa atual
Eu tenho um aplicativo que eu quero rodar em segundo plano. Eu quero obter o nome do executável, para um exemplo de IExplorer.exe. Eu brinquei com o seguinte 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);
}
}
Isso só me dá o título da janela e o identificador de identificador. Eu quero pegar o nome do executável (e talvez mais informações).
Como faço para conseguir isso?