C# : Get the all running processes and applications

This program will prints all the running processes and applications on your machine. It uses System.Management namespace and retrieves the information by querying Win32_Processes.

Complete C# code:
=============
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.ComponentModel;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            GetProcesses();
            GetApplications();
            Console.Read();
        }

        public static void GetProcesses()
        {
            StringBuilder sb = new StringBuilder();
            ManagementClass MgmtClass = new ManagementClass("Win32_Process");

            foreach (ManagementObject mo in MgmtClass.GetInstances())          
                Console.WriteLine("Name:" + mo["Name"] + "ID:" + mo["ProcessId"]);              
           
            Console.WriteLine();
        }

        public static void GetApplications()
        {
            StringBuilder sb = new StringBuilder();
            foreach (Process p in Process.GetProcesses("."))
            {
                try
                {
                    if (p.MainWindowTitle.Length > 0)
                    {
                        Console.WriteLine("Window Title:" + p.MainWindowTitle.ToString());
                        Console.WriteLine("Process Name:" + p.ProcessName.ToString());
                        Console.WriteLine("Window Handle:" + p.MainWindowHandle.ToString());
                        Console.WriteLine("Memory Allocation:" + p.PrivateMemorySize64.ToString());                    
                    }
                }
                catch { }
            }           
        }
    }
}

Example Output on my machine:
================
Name:System Idle ProcessID:0
Name:SystemID:4
Name:explorer.exeID:532
Name:smax4pnp.exeID:2880
Name:DVDLAU~1.EXEID:2580
Name:winampa.exeID:2140
Name:OUTLOOK.EXEID:9544
Name:ccSvcHst.exeID:30580
Name:ccApp.exeID:31680

Window Title:ConsoleApplication1 (Running) - Microsoft Visual Studio
Process Name:devenv
Window Handle:2558386
Memory Allocation:53194752
Window Title:Inbox - Microsoft Outlook
Process Name:OUTLOOK
Window Handle:7209408
Memory Allocation:40906752
Window Title:. Mozilla Firefox
Process Name:firefox
Window Handle:16515780
Memory Allocation:163573760

By Perry    Popularity  (10737 Views)