The*_*urf 4 c# .net-1.1 windows-server-2000
我的代码需要确定特定进程运行了多长时间。但它继续失败,并在Process.StartTime请求中显示拒绝访问的错误消息。这是一个使用用户凭据运行的进程(即,不是高特权进程)。这里显然是一个安全设置或策略设置,或东西,我需要解决这个问题,因为我不能相信的开始时间属性在框架刚刚以便它可以失败的100%的时间来摆弄。
Google 搜索表明我可以通过将运行查询代码的凭据的用户添加到“性能日志用户”组来解决此问题。但是,这台机器上不存在这样的用户组。
我读过一些类似于你过去所说的话,拉斯。不幸的是,我对有关机器的功能有些限制(换句话说,我不能随意创建用户组:它是一个服务器,而不仅仅是一些随机的 PC)。
感谢威尔和拉斯的回答。不幸的是,他们没有解决我的问题。
对此的最终解决方案是使用 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);
Run Code Online (Sandbox Code Playgroud)
部分内容来自 Code Project:
http://www.codeproject.com/KB/system/win32processusingwmi.aspx
以及“嘿,脚本专家!”:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/jul05/hey0720.mspx
底层代码需要能够调用 OpenProcess,为此您可能需要 SeDebugPrivilege。
您正在执行 StartTime 请求的进程是否以与您自己的进程不同的用户身份运行?