C # uzyskać informacje o bieżącym aktywnym oknie
Mam aplikację, którą chcę uruchomić w tle. Chcę uzyskać nazwę pliku wykonywalnego, na przykład IExplorer.exe. Grałem z następującym kodem:
[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);
}
}
Otrzymuję tylko tytuł okna i identyfikator uchwytu. Chcę uzyskać nazwę pliku wykonywalnego (i może więcej informacji).
Jak to osiągnąć?