当 Visual Studio 处于管理员模式时出现此错误
System.ComponentModel.Win32Exception
HResult=0x80004005
Message=Access is denied
Source=System
StackTrace:
at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.GetProcessTimes()
at System.Diagnostics.Process.get_StartTime()
at TaskManager.Program.Main(String[] args) in C:\Users\User\source\repos\TaskManager\TaskManager\Program.cs:line 18
我只是想显示一个进程的总运行时间。这是主要代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace TaskManager
{
class Program
{
static void Main(string[] args)
{
Process[] processList = Process.GetProcesses();
foreach(Process process in processList)
{
DateTime localDate = DateTime.Now;
Console.WriteLine(@" {0} | ID: {1} | Status {2} | Memory {3} MB | Total Run Time {4}", process.ProcessName, process.Id, process.Responding, process.PrivateMemorySize64 / 1000000, localDate - process.StartTime);
}
Console.ReadLine();
}
}
}
谢谢