c # - .NET Process.GetProcesses () 'Adgang nægtes.'

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg forsøger at få en liste over processer, der kører på den aktuelle maskine ved hjælp af nedenstående kode:


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;

        }
    }
}


Jeg får nedenstående fejl:


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


Indtast billedbeskrivelse her [5]


Har jeg brug for en slags forhøjede rettigheder inden for koden? Hvis ja, hvordan gør jeg det her?

Bedste reference


Prøv at finde ud af, hvilken proces det er. Måske er det en systemproces, du ikke behøver. Afhængigt af dit behov selvfølgelig. Du kan også prøve at køre din ansøgning med forhøjede rettigheder (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);
    }
}