A simple question about Registry CreateSubKey

Why is this not working? I am trying to create a registry key in the [HKEY_LOCAL_MACHINE \ SOFTWARE \ MyService] section, but nothing was created.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using Microsoft.Win32; namespace RegistryKey { class Program { static void Main(string[] args) { const string SUB_KEY_NAME = @"SOFTWARE\MyService"; // Create a subkey named MyService under HKEY_LOCAL_MACHINE. Registry.LocalMachine.CreateSubKey(SUB_KEY_NAME); } } } 

Update: Nothing. I am an idiot. I used the remote registry editor to check the registry, believing that it will display the same as regedit. This is not true! I see the way using regedit.

+4
source share
2 answers

You do not have write access to HKLM. If you want to write here, you need to either:

  • start the process as an elevated user or
  • Try to write only HKLM during installation.
+7
source

Try using this code:

 RegistryKey regkey = Registry.CurrentUser; regkey = regkey.CreateSubKey(SUB_KEY_NAME); //this is the path then you create yours keys regkey.SetValue("Install", "ok"); //name of key exp:(install) and then the value exp:(ok) 
+1
source

Source: https://habr.com/ru/post/1342603/


All Articles