55

I'm trying to write to the registry using my C# app.

I'm using the answer given here: Writing values to the registry with C#

However for some reason the key isn't added to the registry.

I'm using the following code:

string Timestamp = DateTime.Now.ToString("dd-MM-yyyy");

string key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\"+Application.ProductName+"\\"+Application.ProductVersion;
string valueName = "Trial Period";

Microsoft.Win32.Registry.SetValue(key, valueName, Timestamp, Microsoft.Win32.RegistryValueKind.String);

The Application.name and Application.version 'folders' don't exists yet.

Do I have to create them first?

Also, I'm testing it on a 64b Win version so I think if I want to check the registry for the key added I have to specifically check the 32bit registry in: C:\Windows\SysWOW64\regedit.exe don't I?

2
  • 3
    UAC is going to ruin your plans, you cannot write to HKLM without elevation. Unless you write an installer that changes the accessibility of the key. License enforcement code is the kind of code you buy. It takes a penny to make a penny. Commented Aug 29, 2011 at 12:50
  • you should use boxedapp. it must help you.
    – John Smith
    Commented Dec 22, 2011 at 19:00

6 Answers 6

78

First of all if you want to edit key under LocalMachine you must run your application under admin rights (better use CurrentUser it's safer or create the key in installer). You have to open key in edit mode too (OpenSubKey method) to add new subkeys. I've checked the code and it works. Here is the code.

RegistryKey key = Registry.LocalMachine.OpenSubKey("Software",true);

key.CreateSubKey("AppName");
key = key.OpenSubKey("AppName", true);


key.CreateSubKey("AppVersion");
key = key.OpenSubKey("AppVersion", true);

key.SetValue("yourkey", "yourvalue");
5
  • 6
    It is more secure to write data to the user folder — Microsoft.Win32.Registry.CurrentUser — rather than to the local computer — Microsoft.Win32.Registry.LocalMachine. msdn.microsoft.com/en-us/library/h5e7chcf.aspx Commented Dec 6, 2014 at 20:56
  • I tried doing something basically the same, but using the using methodology. That caused some weird issues when the key was null, so I tried this instead with no problems triggering due to null-key exceptions. Happy day! ^^ Commented Feb 22, 2018 at 22:43
  • Also, take a look here: stackoverflow.com/questions/14956551/… , to avoid problems with x64 Commented Mar 27, 2018 at 19:23
  • 1
    Key is placed under the "SOFTWARE\WOW6432Node\" path
    – Sujoy
    Commented Apr 23, 2019 at 8:53
  • 1
    Have System.Security.SecurityException when use this code Commented Jul 19, 2019 at 16:26
9

You can use the following code to create and open the required registry keys.

RegistryKey SoftwareKey   = Registry.LocalMachine.OpenSubKey("Software",true);

RegistryKey AppNameKey    = SoftwareKey.CreateSubKey("AppName");
RegistryKey AppVersionKey = AppNameKey.CreateSubKey("AppVersion");

AppVersionKey.SetValue("yourkey", "yourvalue");

You can basically use CreateSubKey for all your application settings, as it will open the key for write access, if it already exists, and create it otherwise. There is no need to create first, and then open. OpenSubKey comes in handy when you are absolutely certain the key already exists, like in this case, with "HKEY_LOCAL_MACHINE\SOFTWARE\"

0
7

Also check if your registry calls are getting virtualised. See here for more information.

It can happen if your application is not UAC aware and occurs for compatibility reasons.

Real path
HKEY_LOCAL_MACHINE\Software\FooKey

Virtual path
HKEY_USERS\<User SID>_Classes\VirtualStore\Machine\Software\FooKey
3

Try to open HKLM\Software first. Then create key for your program, and then create key for version. Howewer, your key could be placed at HKLM\software\WOW6432Node. Check this.

1
  • Will it have any impact in read/write of the key, if the key is placed under \WOW6432Node ?
    – Sujoy
    Commented Apr 23, 2019 at 8:52
3

The problem is you don't have enough privileges. Here is a way that works for my:

RegistryKey myKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
myKey = myKey.OpenSubKey(subkey, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);

if (myKey != null)
{
    myKey.SetValue("DefaultPrinterId", ldiPrinters[e.RowIndex].id, RegistryValueKind.String);
    myKey.Close();
}

With RegistryKey.OpenBaseKey you open the correct registry, because when you don't have permissions the registry that you write, it does in another location.

2

By default, your changes will be written to HKLM\SOFTWARE\WOW6432Node\... because of registry redirection. This can be quite confusing.

In order to write to HKLM\SOFTWARE\..., you need to use RegistryKey.OpenBaseKey to open the 64-bit registry:

var path = @"SOFTWARE\...";
var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
var key = baseKey.CreateSubKey(path, RegistryKeyPermissionCheck.ReadWriteSubTree);
key.SetValue(name, value, RegistryValueKind.String);

Also, you need to have permission to write to the specified registry key.

You can get permission either by assigning permissions to specific users or service accounts or by running your app in elevated mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.