我正在尝试使用以下代码获取当前机器上运行的进程列表:
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."}
我需要在代码中获得某种提升的权限吗?如果是这样,我该怎么做?