Process.StartTime Access Denied

前端 未结 5 1374
既然无缘
既然无缘 2020-12-21 04:30

My code needs to determine how long a particular process has been running. But it continues to fail with an access denied error message on the Process.StartTime

5条回答
  •  忘掉有多难
    2020-12-21 04:59

    I've read something similar to what you said in the past, Lars. Unfortunately, I'm somewhat restricted with what I can do with the machine in question (in other words, I can't go creating user groups willy-nilly: it's a server, not just some random PC).

    Thanks for the answers, Will and Lars. Unfortunately, they didn't solve my problem.

    Ultimate solution to this is to use WMI:

    using System.Management;
    String queryString = "select CreationDate from Win32_Process where ProcessId='" + ProcessId + "'";
    SelectQuery query = new SelectQuery(queryString);
    
    ManagementScope scope = new System.Management.ManagementScope(@"\\.\root\CIMV2");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    ManagementObjectCollection processes = searcher.Get();
    
        //... snip ... logic to figure out which of the processes in the collection is the right one goes here
    
    DateTime startTime = ManagementDateTimeConverter.ToDateTime(processes[0]["CreationDate"].ToString());
    TimeSpan uptime = DateTime.Now.Subtract(startTime);
    

    Parts of this were scraped from Code Project:

    http://www.codeproject.com/KB/system/win32processusingwmi.aspx

    And "Hey, Scripting Guy!":

    http://www.microsoft.com/technet/scriptcenter/resources/qanda/jul05/hey0720.mspx

提交回复
热议问题