Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

c# - How can I update the registry using .NET 3.5 (CF)?

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(@"SOFTWAREAndroid 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(@"SOFTWAREMicrosoftWindows 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)?

UPDATE

It turns out I may be barking up the wrong tree with this attempt to update the "GuestOnly" setting.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...