简体   繁体   中英

Win32Exception while in Adminstrator mode

Im getting this error while Visual Studio is in Administrator mode

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

Im just trying to show total time run of a process. Here is the main code

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();
        }
    }
}

Thanks

Certain processes require escalated privileges in order to access certain information (Like StartTime). You can always use a try/catch loop, to skip the processes you can't access:

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)
            {
                try
                {
                    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);
                }
                catch (Win32Exception ex)
                {
                    Console.WriteLine(@"Unable to access process '{0}': {1}", process.Id, ex.Message);
                }
            }
            Console.ReadLine();
        }
    }
}

You can add a manifest file in your solution. Change this line to make the application acquire admin privileges:

<requestedExecutionLevel level="requireAdministrator" 
    uiAccess="false" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM