C# registry problem

  • Thread starter Sean Riker via .NET 247
  • Start date
S

Sean Riker via .NET 247

I'm writing an executable that will either give or take away access to the TaskManager. The problem is some of the computers on my LAN don't have the subkey. I tried to create it but I got this message:

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: Cannot write to the registry key.

Here is my source:


RegistryPermission f = new RegistryPermission(RegistryPermissionAccess.AllAccess, "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies");

//Get the names of the subkeys to determine if the subkey "System" already exists.
RegistryKey temp = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies");
foreach(string subKeyName in temp.GetSubKeyNames())
{
if(subKeyName == "System")
{
RegistryKey nrk = temp.OpenSubKey("System" ,true);
nrk.SetValue("DisableTaskMgr","1");
nrk.Close();
}
else
{
RegistryPermission P = new RegistryPermission(RegistryPermissionAccess.Write, "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies");
---------------->>> RegistryKey nrk = temp.CreateSubKey("System");
nrk.SetValue("DisableTaskMgr","1");
nrk.Close();
}

As you can see I should theoretically have access because I set the permissions with:
RegistryPermission f = new RegistryPermission(RegistryPermissionAccess.AllAccess, "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies");

Any help would be appreciated.
 
M

Mattias Sjögren

Sean,
RegistryKey temp = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies");

You have to open this with write access (pass true as the second
parameter) to create subkeys.

As you can see I should theoretically have access because I set the permissions with:
RegistryPermission f = new RegistryPermission(RegistryPermissionAccess.AllAccess, "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies");

No, this line of code doesn't set any permissions. In fact, it doesn't
do much at all since you never use the f object.



Mattias
 

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