Search code examples
c#windowsregistry

C# pretends to change registry?


Let me preface this by saying I have no idea what's happening. I simply want to change a registry key from localhost to 127.0.0.1. I wrote some C# to quickly change it and it seems to work, except for when I refresh regedit to view the changes.

try {   
     var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE");
     key = key.OpenSubKey(@"ServOr", true);

     //SHOWS localhost
     MessageBox.Show(key.GetValue(@"HOST").ToString());
     key.SetValue(@"HOST", @"127.0.0.1", RegistryValueKind.String);

     //SHOWS 127.0.0.1
     MessageBox.Show(key.GetValue(@"HOST").ToString());

     key.Close();
     //Regedit still shows localhost
} catch (Exception exception) {
     MessageBox.Show(exception.ToString());
}

The particularly strange part that is if you run it a second time, both Messageboxes will show 127.0.0.1 although the registry still has localhost. Visual studio is running with admin privileges. The registry allows full control to admins.


Solution

  • When you run a 32-bit process in 64-bit Windows, Windows will hide the 64-bit registry and Program Files directory (among other things) from the program. When a 32-bit program access the registry, it will show it a subset of the registry in the Wow6432Node path.

    So, your key is in HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ServOr

    Check out http://en.wikipedia.org/wiki/WoW64 and this post on MSDN