LoveMeSomeCode LoveMeSomeCode -4 years ago 124
ASP.NET (C#) Question

System.Management access denied

I was using some code to try to count the number of processors in .NET 2.0:

internal static int GetNumberOfProcessors()
{
List<string> list = new List<string>();
ManagementClass mgmt = new ManagementClass("Win32_Processor");
foreach (ManagementObject obj in mgmt.GetInstances())
{
string item = obj.Properties["SocketDesignation"].Value.ToString();
if (!list.Contains(item))
{
list.Add(item);
}
}
return list.Count;
}


and it blew up like this:

[ManagementException: Access denied ]
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) +377984
System.Management.ManagementScope.InitializeGuts(Object o) +654
System.Management.ManagementScope.Initialize() +162
System.Management.ManagementObject.Initialize(Boolean getObject) +492
System.Management.ManagementClass.GetInstances(EnumerationOptions options) +122
System.Management.ManagementClass.GetInstances() +6


This code runs fine locally on cassini, but blows up on our beta server. Anyone have any idea what to do about this?

Answer Source

Ok, I hate answering my own question, but I found this: http://support.microsoft.com/kb/317012 and it does seem to work.

You have to specifically modify your CIMV2 permissions, and I had to do it for the specific user, not ASPNET. Once this is on, you can use the System.Management code to count processors etc.

I really don't like how obscure this was. Microsoft needs to update those exceptions. When the System.Management class fails like that it should give detailed info on which permissions need activated for the code to work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download