4

I am trying to create a registry key at following location but I am getting access denied error:

HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany\MyProgram

Here is the code:

RegistryKey reg; reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MyCompany\MyProgram"); 
2
  • Probably the user that is executing your code actually does not have the permissions to create the key. If you manually start regedit with the same user, can you create the subkey in the Registry Editor? Commented Apr 14, 2012 at 7:10
  • @UweKeim I am logged in as administrator. But when I run regedit then I get that message from Windows "Do you want to allow the program to make changes to the computer?" and when I press Yes only then I can access registry. And yes I can create key and subkey through registry editor. Commented Apr 14, 2012 at 7:17

3 Answers 3

7

You are most likely using User Account Control (UAC). This means that even if you are an administrator your access token doesn't have the necessary privileges to do things like creating registry keys in HKEY_LOCAL_MACHINE.

However, by going through a UAC prompt you can elevate your privileges.

Regedit includes a UAC manifest that will raise the prompt before it is executed ensuring that it can perform the actions it needs to be able to do. You can also right-click on an executable or shortcut and select Run as administrator.

So essentially you have three options:

  • Turn off UAC
  • Use Run as administrator
  • Include a UAC manifest in your executable

The first solution is less secure and the last solution is the most elegant (but also the one that actually requires some effort).

Sign up to request clarification or add additional context in comments.

2 Comments

My solution to write to HKLM was to "Run as administrator" on a spinoff process from the same executable (passing an argument so that the exe would do something different). However, I also had to set the Platform target setting to "Any CPU" instead of "x86".
@DanW: When you set the platform target to Any CPU on .NET 4.0 and execute your application on a 64 bit operating system the process will be a 64 bit process. This means that your application sees the "real" registry. If it executes in a 32 bit process the process sees a modified version of the registry and that is probably why you have to change the platform target.
5

You need to run your application under an account that has sufficient privileges to write to the registry at the specified location. Usually the HKEY_LOCAL_MACHINE branch is reserved for power users because it contains machine global settings.

7 Comments

I am logged in as administrator. But when I run regedit then I get that message from Windows "Do you want to allow the program to make changes to the computer?" and when I press Yes only then I can access registry.
@Ali, so you have to run your application as Administrator, exactly the same way you are doing with regedit. You right click on the executable and select Run As administrator.
@Ali - just because you are logged in as admin, your apps do not run at that privilege level automatically, you have to get them to request elevated privileges.
You can't do that in C#. It's the user running your application that must start it as administrator. All you can do is include a manifest in your application so that when the user starts it, it prompts for elevating privileges.
You can do that in C# by adding a Application Manifest File and then un-comment the following line: <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
|
1

I tried below syntax:

SETX /S [Machine Name] [Variable] [Value] /M 

I am not sure why but in Windows 7, if you specify machine name on which you are adding system variable, even for local machine then it works.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.