Access to the registry key 'Global' is denied

I'm using Visual C# 2005 Express to write a program that checks for available RAM.

The partial code is as below:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Diagnostics;

namespace MyTests

{

publicpartialclassForm1 :Form

{

public Form1()

{

InitializeComponent();

}

privatevoid buttonCheckRam_Click(object sender,EventArgs e)

{

PerformanceCounter ramCounter =newPerformanceCounter("Memory","Available MBytes");

MessageBox.Show(ramCounter.NextValue() +"MB");

}

}

}

Upon running the program as Administrator, there was no problem, but when I run the program as a normal user, it

throws an "UnauthorizedAccessException" , Acess to the registry key 'Global' is Denied.

Any ideas, how to solve this problem is much appreciated.

Paul

[2938 byte] By [PaulChin] at [2007-12-28]
# 1

Are you running Windows Vista? I had the same issue and discovered the UAC (User Access Control) was enabled. Disabling it cleared my problem.

In Vista, Start --> Control Panel --> Users Accounts --> User Account --> Turn User Account Control On....

Hope this helps.

Ckiszka at 2007-9-4 > top of Msdn Tech,Software Development for Windows Vista,Security for Applications in Windows Vista...
# 2

Is there anything in code that you can write to just permit this PerformanceCounter to run? I read something about a PerformanceCounterPermission class, but can't find a good example on how to apply it to the PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");. I run windows vista, and the application does not run as administrator and I don't want to change anything manually on the computer, only in the C# code.

saga at 2007-9-4 > top of Msdn Tech,Software Development for Windows Vista,Security for Applications in Windows Vista...

Software Development for Windows Vista

Site Classified