c# – I need to get the Task Manager process description

Question:

I need to get the name of the process that is active on the screen, but I need to bring the same appears in the Task Manager in description.

For example if I use processName it will bring "Chrome" I need it to be description = Google Chrome.

I've tried it like this:

 foreach (Process p in Process.GetProcesses())
                {


                    if (p.MainWindowTitle.Length > 0)
                    {


                        if (app.NomeAplicativo.Contains(p.ProcessName))
                        {
                            Console.WriteLine("Process Name:" + p.ProcessName.ToString());
                        }

                        Console.WriteLine("Process Name:" + p.ProcessName);
                    }
                }

And using this guy:

  public static Int32 GetWindowProcessID(IntPtr hwnd)
        {
            // Esta função é usada para obter o AID do processo ativo ...
            Int32 pid;
            GetWindowThreadProcessId(hwnd, out pid);
            return pid;
        }

But in both I can't get the description of the process.

Answer:

According to this answer in the OS it has a form that solves in most situations, but not guaranteed. Need to use Windows API functions that are not normally available in .Net:

[DllImport("user32.dll")]
public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);

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

string GetActiveProcessFileName() {
    IntPtr hwnd = GetForegroundWindow();
    uint pid;
    GetWindowThreadProcessId(hwnd, out pid);
    Process p = Process.GetProcessById((int)pid);
    p.MainModule.FileName.Dump();
}

I put it on GitHub for future reference .

Scroll to Top