3

I am trying to get a list of processes running on the current machine using the below code:

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;
using System.ComponentModel;

namespace Xynfo.Classes
{
    class Processes
    {
        //Gets list of processes running on local machine
        public static Process[] ProcessList = Process.GetProcesses(Environment.MachineName);

        //Creates the data table that will hold the process data
        public static DataTable ProcessTable = new DataTable();


         public  DataTable GetProcessesInfo()
        {

            //Create DataTable Columns

            ProcessTable.Columns.Add("Name", typeof(string));
            ProcessTable.Columns.Add("Start Time", typeof(DateTime));
            ProcessTable.Columns.Add("CPU %", typeof(TimeSpan));
            ProcessTable.Columns.Add("Threads", typeof(string));
            ProcessTable.Columns.Add("Session ID", typeof(int));
            ProcessTable.Columns.Add("Unique ID", typeof(int));
            ProcessTable.Columns.Add("RAM", typeof(float));
            ProcessTable.Columns.Add("Machine", typeof(string));
            ProcessTable.Columns.Add("Priority", typeof(int));




            foreach (Process Process in ProcessList)
            {
                string pName = Process.ProcessName;
                DateTime pStartTime = Process.StartTime;
                TimeSpan pProcTime = Process.TotalProcessorTime;
                string pThreads = Process.Threads.ToString();
                int pSessionId = Process.SessionId;
                int pId = Process.Id;
                long pRam = Process.VirtualMemorySize64;
                string pMachineName = Process.MachineName;
                int pPriority = Process.BasePriority;



                ProcessTable.Rows.Add(Process.ProcessName
                                     ,Process.StartTime
                                     ,Process.TotalProcessorTime
                                     ,Process.Threads
                                     ,Process.SessionId
                                     ,Process.Id
                                     ,Process.VirtualMemorySize64
                                     ,Process.MachineName
                                     ,Process.BasePriority);
            }

            return ProcessTable;

        }
    }
}

I am getting the below error:

System.ComponentModel.Win32Exception {"Access is denied."}

enter image description here

Do I need some kind of elevated privileges within the code? If so how do I do this?

Brian
  • 1,951
  • 16
  • 56
  • 101
  • 3
    As far as I remember doing the same thing, for some system processes not even administrative privileges are sufficient. – Zoran Horvat Jan 14 '17 at 23:49
  • Try ti include the stack trace to see what line fails, but I think @ZoranHorvat is right. – Michael Jan 14 '17 at 23:52
  • Windows has restrictions on what data you can query from processes. You will have to properly handle the exceptions and show what are not allowed to the end users. – Lex Li Jan 15 '17 at 04:02

1 Answers1

2

Try to figure out which process it is. Maybe it is a system process that you don't need. Depends of your need of course. You can also try to run you application with elevated privileges (Right click exe->Run as Administrator)

foreach (Process Process in ProcessList)
{
    try
    {
        string pName = Process.ProcessName;
        DateTime pStartTime = Process.StartTime;
        TimeSpan pProcTime = Process.TotalProcessorTime;
        string pThreads = Process.Threads.ToString();
        int pSessionId = Process.SessionId;
        int pId = Process.Id;
        long pRam = Process.VirtualMemorySize64;
        string pMachineName = Process.MachineName;
        int pPriority = Process.BasePriority;



        ProcessTable.Rows.Add(Process.ProcessName
                             ,Process.StartTime
                             ,Process.TotalProcessorTime
                             ,Process.Threads
                             ,Process.SessionId
                             ,Process.Id
                             ,Process.VirtualMemorySize64
                             ,Process.MachineName
                             ,Process.BasePriority);
    }
    catch(Win32Exception e)
    {
        logger.LogWarning($"Error reading process {process.ProcessName}", e.Message);
    }
}
Michael
  • 3,350
  • 2
  • 21
  • 35