53

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?

Community
  • 1
  • 1
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • 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. – Hans Passant Aug 29 '11 at 12:50
  • you should use boxedapp. it must help you. – John Smith Dec 22 '11 at 19:00

6 Answers6

77

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");
Silx
  • 2,663
  • 20
  • 21
  • 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. http://msdn.microsoft.com/en-us/library/h5e7chcf.aspx – Gjermund Dahl Dec 06 '14 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! ^^ – kayleeFrye_onDeck Feb 22 '18 at 22:43
  • Also, take a look here: https://stackoverflow.com/questions/14956551/c-sharp-add-key-to-registry-to-localmachine-fails , to avoid problems with x64 – Jonatan Dragon Mar 27 '18 at 19:23
  • 1
    Key is placed under the "SOFTWARE\WOW6432Node\" path – Sujoy Apr 23 '19 at 08:53
  • 1
    Have System.Security.SecurityException when use this code – HelloWindowsPhone Jul 19 '19 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\"

Wracky
  • 581
  • 6
  • 5
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
TheCodeKing
  • 19,064
  • 3
  • 47
  • 70
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.

Rey Cruz
  • 384
  • 3
  • 14
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.

Viktorianec
  • 361
  • 6
  • 22
  • Will it have any impact in read/write of the key, if the key is placed under `\WOW6432Node` ? – Sujoy Apr 23 '19 at 08:52
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.

Vinz
  • 89
  • 1
  • 8
pberggreen
  • 928
  • 6
  • 13