Process.GetAllProcesses C# (CSharp) Method

GetAllProcesses() public static method

Get a list of all the processes running on the system at a given time.
public static GetAllProcesses ( ) : List
return List
    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;
    }