static void PrintProcEntry(PROCESSENTRY32 procEntry) { Console.WriteLine("Process Entry"); Console.WriteLine("============================="); Console.WriteLine("File: {0}",procEntry.szExeFile); Console.WriteLine("Thread Count: {0}", procEntry.cntThreads); Console.WriteLine(""); }
/// <summary> /// Get a list of all the processes running on the system at a given time. /// </summary> /// <returns>The list of all the processes on the system at the time of the call.</returns> public static List<PROCESSENTRY32> GetAllProcesses() { IntPtr snapshot = Kernel32.CreateToolhelp32Snapshot((uint)SnapshotType.TH32CS_SNAPPROCESS, 0); List<PROCESSENTRY32> procList = new List<PROCESSENTRY32>(); PROCESSENTRY32 procEntry = new PROCESSENTRY32(); procEntry.dwSize = (uint)Marshal.SizeOf(procEntry); // don't forget to set the structure size bool success = Kernel32.Process32First(snapshot, ref procEntry); uint error = Kernel32.GetLastError(); if (success) { procList.Add(procEntry); procEntry = new PROCESSENTRY32(); procEntry.dwSize = (uint)Marshal.SizeOf(procEntry); while (Kernel32.Process32Next(snapshot, ref procEntry)) { procList.Add(procEntry); } } Kernel32.CloseHandle(snapshot); return procList; }