3

I've had no trouble creating/deleting keys to the CurrentUser but whenever I try LocalMachine nothing happens. I've added ("requestedExecutionLevel level="requireAdministrator" uiAccess="false") to the app.manifest. Am I missing something?

    private void button1_Click(object sender, EventArgs e)
    {
        RegistryKey rKey;
        rKey = Registry.LocalMachine.OpenSubKey("Software", true);
        rKey.DeleteSubKey("test", true);
        rKey.Close();            
    }

    private void button2_Click(object sender, EventArgs e)
    {
        RegistryKey rKey;
        rKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
        rKey.CreateSubKey("test");
        rKey.Close();
    }
anothermh
  • 9,815
  • 3
  • 33
  • 52
  • If you are seeking for debugging help then it would be of help to see working method, not working method and description of what is not working (e.g. you are not checking for `null` anywhere). Wild guess - [64 bit problem](http://stackoverflow.com/q/1268715/1997232). – Sinatr Aug 16 '16 at 16:06
  • I will try the 64bit solution that seems rational. But the working method just replaces LocalMachine with CurrentUser – user6722600 Aug 16 '16 at 16:08
  • are you sure the key exists? – Steve Aug 16 '16 at 16:08
  • Check to see if it's creating/removing keys in HKLM\SOFTWARE\Wow6432Node\ – Phylogenesis Aug 16 '16 at 16:09
  • Thank you all very much for the quick reply's! Phylogenesis hit the nail on the head it was being created in Wow6432Node I'm such a dummy. It raises another question though. How do I create / delete a key inside of just HKLM\SOFTWARE\ – user6722600 Aug 16 '16 at 16:14
  • 3
    @user6722600 Create a 64-bit build of your application. 32-bit applications on 64-bit Windows have their HKLM\Software keys virtualized inside the Wow6432Node key. – Phylogenesis Aug 16 '16 at 16:18
  • 1
    You really should not be calling `.Close()`, you should be using `using(RegistryKey rKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true)) { rKey.CreateSubKey("test"); }` so the object will be disposed even if there is a error. – Scott Chamberlain Aug 16 '16 at 16:30
  • @Phylogenesis Awesome alright I'll google how to do that. Also while I have you if you don't mind is it possible to add / delete multiple keys in one command? I am fairly new to this. – user6722600 Aug 16 '16 at 16:34
  • @ScottChamberlain I'll keep that in mind thank you very much. – user6722600 Aug 16 '16 at 16:40
  • @Phylogenesis you should add your comment as an Answer. – Martin Brown Aug 17 '16 at 11:14
  • https://stackoverflow.com/questions/14956551/c-sharp-add-key-to-registry-to-localmachine-fails – Jonatan Dragon Mar 27 '18 at 19:19

1 Answers1

2

As discussed in the comments above, 32-bit applications on 64-bit Windows virtualize the HKLM\SOFTWARE key inside HKLM\SOFTWARE\Wow6432Node for compatibility reasons.

If you want the keys to actually be created inside HKLM\SOFTWARE you will need to rebuild your application as 64-bit.

In my opinion, however, it really shouldn't matter where the keys are actually stored as long as you can actually load and save keys.

Phylogenesis
  • 7,775
  • 19
  • 27
  • Ignore the last post I was just being stupid forgot to check something. Please up vote all of his posts and add them to the answers list thank you very much for your help! – user6722600 Aug 17 '16 at 18:17