Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple question about Registry CreateSubKey

Why doesn't this work? I am trying to create a registry key under [HKEY_LOCAL_MACHINE\SOFTWARE\MyService], but nothing is 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: Never mind. I'm an idiot. I used the Remote registry Editor to inspect the registry in the belief that it would show the same as regedit. It didn't! I can see the path using regedit.

like image 942
Kasper Hansen Avatar asked Feb 25 '23 21:02

Kasper Hansen


1 Answers

You don't have write access to HKLM. If you want to write here then you need to either:

  • run the process as an elevated user, or.
  • only attempt to write to HKLM during install.
like image 178
David Heffernan Avatar answered Feb 27 '23 09:02

David Heffernan