5

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 request. This is a process running with a User's credentials (ie, not a high-privilege process). There's clearly a security setting or a policy setting, or something that I need to twiddle with to fix this, as I can't believe the StartTime property is in the Framework just so that it can fail 100% of the time.

A Google search indicated that I could resolve this by adding the user whose credentials the querying code is running under to the "Performance Log Users" group. However, no such user group exists on this machine.

4

5 回答 5

6

拉斯,我读到了与你过去所说的类似的内容。不幸的是,我对有问题的机器可以做什么有些限制(换句话说,我不能随便创建用户组:它是一个服务器,而不仅仅是一些随机的 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);

其中部分内容是从 Code Project 中抓取的:

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

还有“嘿,脚本专家!”:

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

于 2008-08-28T07:30:55.510 回答
2

.Net 1.1 的进程使用性能计数器来获取信息。它们被禁用或用户没有管理权限。确保启用了性能计数器并且用户是管理员应该使您的代码工作。

实际上“性能计数器用户组”应该足够了。默认情况下该组不存在。所以你应该自己创建它。

.Net 2.0 的进程不依赖于性能计数器。

http://weblogs.asp.net/nunitaddin/archive/2004/11/21/267559.aspx

于 2008-08-26T19:49:18.293 回答
1

The underlying code needs to be able to call OpenProcess, for which you may require SeDebugPrivilege.

Is the process you're doing the StartTime request on running as a different user to your own process?

于 2008-08-26T17:56:10.983 回答
0

好的,抱歉,这不起作用...我不是 ASP.NET 模拟方面的专家,我倾向于使用我认为您无法在 W2K 上执行的应用程序池您是否尝试过编写一个很小的测试应用程序来执行相同的查询,然后以不同的用户身份运行?

我不愿意在这里发布一大块 MS 框架代码,但您可以使用 Reflector 或这个: http: //www.codeplex.com/NetMassDownloader来获取框架相关位的源代码,以便您可以尝试实现各种位以查看失败的位置。

您能否在不拒绝访问的情况下获得有关该过程的任何其他信息?

于 2008-08-26T19:12:07.760 回答
0

我可以枚举进程(即 GetProcessById 函数有效),我们还有其他代码可以获取 EXE 名称和其他信息。

我会试试这个测试应用程序。如果我无法让 C# 实现在短时间内正常工作,我还将尝试使用 WMI 来获取此信息(这不是关键功能,所以我不能花几天时间在上面)。

于 2008-08-26T19:23:44.017 回答