Access denied issues under Vista while using C# Registry classes

  • Thread starter Thread starter Dilip
  • Start date Start date
D

Dilip

This is driving me batty. Why is this piece of code repeatedly
throwing an "access to registry key is denied exception"?

This is a small console application that is being run on Vista. I
fire up a command prompt (which is NOT running as an administrator)
and when I launch this app the above exception is promptly thrown. Am
I not granting sufficient permissions to the logged on user before
performing OpenSubKey?

string _loggedOnUser = Environment.UserDomainName + "\\" +
Environment.UserName;
RegistrySecurity _regSecurity = new RegistrySecurity();
RegistryAccessRule accessRule =
new RegistryAccessRule(_loggedOnUser,

RegistryRights.ReadKey | RegistryRights.WriteKey |
RegistryRights.Delete,

InheritanceFlags.ContainerInherit,

PropagationFlags.InheritOnly,

AccessControlType.Allow);
_regSecurity.AddAccessRule(accessRule);
_regKey.CreateSubKey(@"SOFTWARE\MySubKey",
RegistryKeyPermissionCheck.ReadWriteSubTree, _regSecurity);

The above code in real life is sitting within a static constructor of
a static class but I guess that is irrelevant.
 
Dilip said:
This is driving me batty. Why is this piece of code repeatedly
throwing an "access to registry key is denied exception"?

This is a small console application that is being run on Vista. I
fire up a command prompt (which is NOT running as an administrator)
and when I launch this app the above exception is promptly thrown. Am
I not granting sufficient permissions to the logged on user before
performing OpenSubKey?

string _loggedOnUser = Environment.UserDomainName + "\\" +
Environment.UserName;
RegistrySecurity _regSecurity = new RegistrySecurity();
RegistryAccessRule accessRule =
new RegistryAccessRule(_loggedOnUser,

RegistryRights.ReadKey | RegistryRights.WriteKey |
RegistryRights.Delete,

InheritanceFlags.ContainerInherit,

PropagationFlags.InheritOnly,

AccessControlType.Allow);
_regSecurity.AddAccessRule(accessRule);
_regKey.CreateSubKey(@"SOFTWARE\MySubKey",
RegistryKeyPermissionCheck.ReadWriteSubTree, _regSecurity);

The above code in real life is sitting within a static constructor of
a static class but I guess that is irrelevant.



You can't "CreateSubkey" or change the access permission on HKLM\... when
running as normal user. You need to run "elevated", however, only installers
should be allowed to do so.

Willy.
 

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

Similar Threads

Dump of perfection Test 1

Back
Top