Access denied issues under Vista while using C# Registry classes

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.
 
W

Willy Denoyette [MVP]

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.
 
Top