52

I'm writing a tweak utility that modifies some keys under HKEY_CLASSES_ROOT.

All works fine under Windows XP and so on. But I'm getting error Requested registry access is not allowed under Windows 7. Vista and 2008 I guess too.

How should I modify my code to add UAC support?

abatishchev
  • 98,240
  • 88
  • 296
  • 433

7 Answers7

64

app.manifest should be like this:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
      <security>
         <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
         </requestedPrivileges>
      </security>
   </trustInfo>
</asmv1:assembly>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 4
    @Gunner: Put it into the root of a project, like App.config. (File -> Add New Item -> Application Manifest File) – abatishchev Apr 20 '11 at 15:21
  • @abatishchev: The above xml content is a generic one and should suffice for any application, right? – Shamim Hafiz - MSFT Apr 20 '11 at 15:46
  • @Gunner: Probably you need to update version and app name. Everything else - should be left as is – abatishchev Apr 20 '11 at 18:46
  • @abatishchev: A bit more clarification.. Do we add this file to our main project? Is the app name same as my main project name? What are we expected to see when installation occurs? – Shamim Hafiz - MSFT May 10 '11 at 10:48
  • 2
    @Gunner: Add `App.manifest` to VS project's root, and it will automatically copied to output as `.exe.manifest` or embedded into assembly – abatishchev May 10 '11 at 11:08
  • @abatishchev: Thanks, about the name attribute in assemblyIdentity, what should I use? – Shamim Hafiz - MSFT May 10 '11 at 11:10
  • 1
    @Gunner: Theoretically should be equal to executable's name – abatishchev May 10 '11 at 11:47
  • Does it mean that user will be always annoyed by UAC warning every time he starts my application? – Laserson Aug 27 '11 at 16:10
  • @Laserson: Yes, it does, he will be always on startup. To avoid that, you need separate the code requiring elevated privileges into separate module, e.g. see [this article](http://www.codeproject.com/KB/vista-security/ElevatedPrivilegesDemand.aspx) – abatishchev Aug 27 '11 at 17:16
  • Surely users should not require admin privs to just read the registry? Many corporate PCs give their users no admin rights, so how could any apps use the registry in that case? – NickG Jul 31 '15 at 08:47
  • @NickG: non privileged user has access to user registry hive only – abatishchev Jul 31 '15 at 16:17
20

You can't write to the HKCR (or HKLM) hives in Vista and newer versions of Windows unless you have administrative privileges. Therefore, you'll either need to be logged in as an Administrator before you run your utility, give it a manifest that says it requires Administrator level (which will prompt the user for Admin login info), or quit changing things in places that non-Administrators shouldn't be playing. :-)

EricLaw
  • 56,563
  • 7
  • 151
  • 196
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 3
    You're welcome. :-) Sorry I couldn't post the proper manifest, but I didn't have one on this machine and figured if someone had to search for one, it might as well be you. – Ken White Feb 18 '09 at 19:01
19

If you don't need admin privs for the entire app, or only for a few infrequent changes you can do the changes in a new process and launch it using:

Process.StartInfo.UseShellExecute = true;
Process.StartInfo.Verb = "runas";

which will run the process as admin to do whatever you need with the registry, but return to your app with the normal priviledges. This way it doesn't prompt the user with a UAC dialog every time it launches.

Davy8
  • 30,868
  • 25
  • 115
  • 173
  • Do you mean that it requires to implement a fork, where one part of code launches the same application with parameter so another part of code will be executed? – abatishchev Feb 28 '09 at 23:35
  • It could be the same app with parameters or it could be a separate small windowless app that writes what it needs. – Davy8 Dec 17 '09 at 15:18
7

As a temporary fix, users can right click the utility and select "Run as administrator."

Brian
  • 25,523
  • 18
  • 82
  • 173
2

I was trying the verb = "runas", but I still was getting UnauthorizedAccessException when trying to update registry value. Turned out it was due to not opening the subkey with writeable set to true.

Registry.OpenSubKey("KeyName", true);

Cannot write to Registry Key, getting UnauthorizedAccessException

Community
  • 1
  • 1
Despertar
  • 21,627
  • 11
  • 81
  • 79
1

This issue has to do with granting the necessary authorization to the user account the application runs on. To read a similar situation and a detailed response for the correct solution, as documented by Microsoft, feel free to visit this post: http://rambletech.wordpress.com/2011/10/17/requested-registry-access-is-not-allowed/

Ozzie
  • 86
  • 1
  • 2
0

You Could Do The same as abatishchev but without the UAC

<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
 <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
   <security>
    <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
    </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>
The Sphynx
  • 23
  • 2