Authored in 2011
The following code can be copy pasted directly into a C# class. Calls to getCurrentAppName() will return a string with the name of the current application.
#region "WinAPI"
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId);
#endregion
private string getCurrentAppName()
{
IntPtr activeAppHandle = GetForegroundWindow();
IntPtr activeAppProcessId;
GetWindowThreadProcessId(activeAppHandle, out activeAppProcessId);
Process currentAppProcess = Process.GetProcessById((int)activeAppProcessId);
string currentAppName = FileVersionInfo.GetVersionInfo(currentAppProcess.MainModule.FileName).FileDescription;
return currentAppName;
}