As can be seen here, with a little help from my friends I was eventually able to update the registry in a .NET 4.5 app using this code:
RegistryKey reg64key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey reg_64bit_AppKey = reg64key.OpenSubKey(@"SOFTWARE\Android Studio", true);
if (reg_64bit_AppKey != null)
{
reg_64bit_AppKey.SetValue("StartMenuGroup", "Droidio", RegistryValueKind.String);
}
else
{
MessageBox.Show("Cannot open registry");
}
...but trying to adapt that test code to work in my.NET 3.5 app doesn't work - it won't even compile. I changed "64" to "32" (it's a Windows CE / Compact Framework app), and changed the specific registry location and value I want to change like so:
private void UpdateRegistry()
{
RegistryKey reg32key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
RegistryKey reg_32bit_AppKey = reg32key.OpenSubKey(@"SOFTWARE\Microsoft\Windows CE Services", true);
if (reg_32bit_AppKey != null)
{
MessageBox.Show(String.Format("value was {0}", reg_32bit_AppKey.GetValue("GuestOnly")));
reg_32bit_AppKey.SetValue("GuestOnly", 00000001, RegistryValueKind.DWord);
MessageBox.Show(String.Format("value is now {0}", reg_32bit_AppKey.GetValue("GuestOnly")));
}
else
{
MessageBox.Show("Cannot open registry");
}
}
...but, as noted, it doesn't compile. As can be seen here, OpenBaseKey is new with .NET 4, and I'm using .NET 3.5 (Compact Framework). RegistryHive also shows "red" in the IDE, but maybe that's because of the problems with OpenBaseKey (according to this, it should be available to me).
RegistryView, like OpenBaseKey, was not even a gleam in the .NET architect's eye at the time .NET 3.5 was released - it first appeared in .NET 4.
So how can I accomplish the same thing I'm doing in .NET 4.5 in .NET 3.5 (without benefit of OpenBaseKey and RegistryView and, perhaps, RegistryHive)?
It turns out I may be barking up the wrong tree with this attempt to update the "GuestOnly" setting.
I simplified my .NET 3.5 code to this:
private void UpdateRegistry()
{
RegistryKey key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows CE Services");
if (key != null)
{
MessageBox.Show(String.Format("value was {0}", key.GetValue("GuestOnly")));
key.SetValue("GuestOnly", 00000001, RegistryValueKind.DWord);
MessageBox.Show(String.Format("value is now {0}", key.GetValue("GuestOnly")));
}
}
...and it seems to work; the first MessageBox showed me "value was " (blank), and the second one was ""value was 1"
Note: That code works for .NET 3.5, but for creakier versions of .NET, the "Registry", "Registry", and "Registry" classes are not available straightaway.
The good news is that the exact same code works if you have OpenNETCF installed/referenced, and use "using OpenNETCF.Win32;"