ManagementClass.GetInstances -> UnauthorizedAccessException !?

G

Guest

Hi,

I can't figure it out. A piece of code to obtain the Processor ID which has
always worked fine, suddenly throws an UnauthorizedAccessException. I have
changed absolutely nothing to my code, and nothing to my system. The same
code works fine on a different computer. The code in question:

string cpuInfo = String.Empty;
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();

foreach (ManagementObject mo in moc)
{
if (cpuInfo == String.Empty)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
}
}

The exception occurs when it tries to iterate through the foreach loop and
halts on the very first iteration.

I am running this on Vista with .NET framework 2.0. Code runs fine on a
similar machine also running Vista.

Any help to point me in the right direction would be much appreciated.

Christopher Hughes
 
N

Nicholas Paldino [.NET/C# MVP]

Christopher,

Could this be a UAC issue perhaps? If you run the code as
administrator, do you get the error?
 
G

Guest

Thanks for your suggestion.

UAC is switched off and I have tried forcing a 'run as administrator'
through a right-click on the executable, but this made no difference.

I thought maybe a Windows update may have sneeked in a problem, but after
uninstalling the most recent update, the error remains. Trying to restore
Windows to the day before unfortunately failed, and I can't think of any
other options.

The fact that this has suddenly occured out of the blue on my development
system is annoying, but the idea that this may just as easily occur on a
customer's system that is running my -working- software, makes it a little
scary...

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top