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.
Peter Bromberg [C# MVP] - 12 Mar 2008 18:17 GMT
I think Willy DeNoyette covered this some time ago by showing how to manifest
your app so it runs "as administrator". I put something about it on my
UnBlog, so here is the link:
http://petesbloggerama.blogspot.com/2007/04/making-your-assembly-run-as.html
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net
> This is driving me batty. Why is this piece of code repeatedly
> throwing an "access to registry key is denied exception"?
[quoted text clipped - 25 lines]
> The above code in real life is sitting within a static constructor of
> a static class but I guess that is irrelevant.
Willy Denoyette [MVP] - 12 Mar 2008 23:58 GMT
> This is driving me batty. Why is this piece of code repeatedly
> throwing an "access to registry key is denied exception"?
[quoted text clipped - 25 lines]
> 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.