.NET Process.GetProcesses()"访问被拒绝。"

时间:2017-01-14 23:47:41

标签: c# windows process system.componentmodel

我正在尝试使用以下代码获取当前计算机上运行的进程列表:

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;

        }
    }
}

我收到以下错误:

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

enter image description here

我是否需要在代码中使用某种提升权限?如果是这样,我该怎么做?

1 个答案:

答案 0 :(得分:1)

尝试确定它是哪个过程。也许这是一个你不需要的系统过程。当然需要你的需要。您还可以尝试使用提升的权限(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);
    }
}
相关问题