Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Process.GetProcesses() "Access is denied."

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?

like image 918
Brian Avatar asked Oct 18 '25 16:10

Brian


1 Answers

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);
    }
}
like image 100
Michael Avatar answered Oct 21 '25 05:10

Michael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!