Show the Process and Application Names in C#

This code snippet shows the process and application names similar to the Task Manager in a console application.

Show the Process Name also Application Name run in TaskManager in C# Consol
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Management;  
  6. using System.Diagnostics;  
  7. using System.Management.Instrumentation;  
  8. namespace getAllProcessID  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             var myID = Process.GetCurrentProcess();  
  15.             var query = string.Format("SELECT ParentProcessId FROM Win32_Process");// WHERE ProcessId = {0}", myID);  
  16.             var search = new ManagementObjectSearcher("root\\CIMV2", query);  
  17.             var results = search.Get().GetEnumerator();  
  18.             if (!results.MoveNext())  
  19.                 throw new Exception("Huh?");  
  20.             var queryObj = results.Current;  
  21.             uint parentId = (uint)queryObj["ParentProcessId"];  
  22.             // this code gives all process run in task manger  
  23.             var parent = Process.GetProcesses(); //.GetProcessById((int)parentId);  
  24.             foreach (Process proc in parent)  
  25.             {  
  26.                 Console.WriteLine("I was started by {0}", proc);  
  27.             }  
  28.             Console.ReadLine();  
  29.         }  
  30.     }  
  31. }  
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Management;  
  6. using System.Diagnostics;  
  7. using System.Management.Instrumentation;  
  8. namespace getAllProcessID  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             var myID = Process.GetCurrentProcess();  
  15.             var query = string.Format("SELECT ParentProcessId FROM Win32_Process");//WHERE ProcessId {0}", myID);  
  16.             var search = new ManagementObjectSearcher("root\\CIMV2", query);  
  17.             var results = search.Get().GetEnumerator();  
  18.             if (!results.MoveNext())  
  19.                 throw new Exception("Huh?");  
  20.             var queryObj = results.Current;  
  21.             uint parentId = (uint)queryObj["ParentProcessId"];  
  22.             foreach (Process p in Process.GetProcesses())  
  23.             {  
  24.                 if (p.MainWindowTitle.Length > 0)  
  25.                 {  
  26.                     Console.WriteLine(p.MainWindowTitle.ToString());  
  27.                     Console.WriteLine(p.ProcessName.ToString());  
  28.                     Console.WriteLine(p.MainWindowHandle.ToString());  
  29.                     Console.WriteLine(p.PrivateMemorySize64.ToString());  
  30.                 }  
  31.             }  
  32.             Console.ReadLine();  
  33.         }  
  34.     }  
  35. }