Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Access denied issues under Vista while using C# Registry classes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dilip - 12 Mar 2008 17:28 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.