Beispiel #1
0
        public WindowsProcess[] GetWindowsProcesses()
        {
            try
            {
                Log.WriteStart("GetWindowsProcesses");

                List <WindowsProcess> winProcesses = new List <WindowsProcess>();

                WmiHelper wmi = new WmiHelper("root\\cimv2");
                ManagementObjectCollection objProcesses = wmi.ExecuteQuery(
                    "SELECT * FROM Win32_Process");

                foreach (ManagementObject objProcess in objProcesses)
                {
                    int    pid  = Int32.Parse(objProcess["ProcessID"].ToString());
                    string name = objProcess["Name"].ToString();

                    // get user info
                    string[] methodParams = new String[2];
                    objProcess.InvokeMethod("GetOwner", (object[])methodParams);
                    string username = methodParams[0];

                    WindowsProcess winProcess = new WindowsProcess();
                    winProcess.Pid      = pid;
                    winProcess.Name     = name;
                    winProcess.Username = username;
                    winProcess.MemUsage = Int64.Parse(objProcess["WorkingSetSize"].ToString());

                    winProcesses.Add(winProcess);
                }

                Log.WriteEnd("GetWindowsProcesses");
                return(winProcesses.ToArray());
            }
            catch (Exception ex)
            {
                Log.WriteError("GetWindowsProcesses", ex);
                throw;
            }
        }
        public WindowsProcess[] GetWindowsProcesses()
        {
            try
            {
                Log.WriteStart("GetWindowsProcesses");

                List<WindowsProcess> winProcesses = new List<WindowsProcess>();

                WmiHelper wmi = new WmiHelper("root\\cimv2");
                ManagementObjectCollection objProcesses = wmi.ExecuteQuery(
                    "SELECT * FROM Win32_Process");

                foreach (ManagementObject objProcess in objProcesses)
                {
                    int pid = Int32.Parse(objProcess["ProcessID"].ToString());
                    string name = objProcess["Name"].ToString();

                    // get user info
                    string[] methodParams = new String[2];
                    objProcess.InvokeMethod("GetOwner", (object[])methodParams);
                    string username = methodParams[0];

                    WindowsProcess winProcess = new WindowsProcess();
                    winProcess.Pid = pid;
                    winProcess.Name = name;
                    winProcess.Username = username;
                    winProcess.MemUsage = Int64.Parse(objProcess["WorkingSetSize"].ToString());

                    winProcesses.Add(winProcess);
                }

                Log.WriteEnd("GetWindowsProcesses");
                return winProcesses.ToArray();
            }
            catch (Exception ex)
            {
                Log.WriteError("GetWindowsProcesses", ex);
                throw;
            }
        }