How to Get Active Process Name in C#?

Viewed 46977

How to get active process name in C#?

I know that I must use this code:

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

but I don't know how use it.

5 Answers
    public void GetProcessNames()
    {
        List<string> windowNames = new List<string>();

        foreach (Process window in Process.GetProcesses())
        {
            if (window.MainWindowHandle != IntPtr.Zero)
            {                    
                windowNames.Add(window.MainWindowTitle);
            }

            // It's that simple
        }
    }
Related