I have an application that under rare circumstances needs to change its registry setting. Also during it’s first execution it needs to create a new key. I’m developing this in Windows 7. I get ThrowUnauthorizedAccessException. How do I force Windows to give me a UAC prompt to temporarily elevate my permissions?
Thanks in advance.
Should all users be allowed to modify this setting? If so, the simplest solution is to modify your installation program to give Users
Full Controlof the registry key.If only administrators should be able to modify this setting, then you will need to launch another copy of your program, asking Windows to elevate it:
If you were smart you would include some command line arguments, so you can tell “yourself” that it should jump straight to the part of the software that the user needs to deal with. Or your command line arguments could just say what you want done:
Have your application check for the
setLoggingEnabledswitch, make the change, and then exit.Update: A common situation is players of World of Warcraft. Since the game is allowed to update itself while running, all users must be allowed to modify the game data sitting in
Program Files. The correct and valid action is to modify the ACLs on thefolder so that all users have full control. In fact, before Blizzard got their act together, Microsoft released an application compatibility update that gives all users full control to the WoW folder next time it run as an administrator.
Another common case is when the Blizzard Launcher is launched with administrative privelages, it updates a registry key in HKLM, recording where the game is. This happens when, for example, i move WoW from a hard drive to an SSD drive
run the launcher once as an administator so that the updaters work correctly.