如何更新使用.NET 3.5(CF)注册表?注册表、NET、CF

2023-09-07 10:43:37 作者:你敢盯着贞子唱忐忑么

如可以看到的here,从我的朋友们一点帮助,我最终能够使用这个code更新注册表在.NET 4.5的应用程序:

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");
}

...但试图去适应这个测试code工作在my.NET 3.5应用程序无法正常工作 - 它甚至不会编译。我改变了64到32(这是Windows CE / Compact Framework的应用程序),并改变我想改变,像这样的特定的注册表位置和值:

...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");
    }
}

...但是,如前所述,它不会编译。如可以看到的此处, OpenBaseKey 的是新的与.NET 4中,和我使用.NET 3.5(Compact Framework的)。 RegistryHive 的也显示在IDE中红,但也许这(根据的这个,应该是提供给我)。

...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 的,喜欢的 OpenBaseKey 的,甚至不是一个在.NET架构的眼睛当时一线.NET 3.5发布 - 它的第一次出现在.NET 4 。

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.

所以,我怎么能完成同样的事情,我在.NET 3.5中做.NET 4.5(无益处的 OpenBaseKey 和 RegistryView 的,也许, RegistryHive 的)?

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)?

原来我可能是barking错了这个尝试更新GuestOnly设置。

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

推荐答案

我简化了我的.NET 3.5 code到这一点:

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")));
    }
}

...这似乎工作;第一个消息框显示我值(空白),而第二个是值1

...and it seems to work; the first MessageBox showed me "value was " (blank), and the second one was ""value was 1"

注意:那code适用于.NET 3.5,但对于creakier版本的.NET中,注册表,注册和注册表类不可用通俗易懂。

Note: That code works for .NET 3.5, but for creakier versions of .NET, the "Registry", "Registry", and "Registry" classes are not available straightaway.

好消息是完全相同的code工作,如果你有OpenNETCF安装/引用,并使用使用OpenNETCF.Win32;

The good news is that the exact same code works if you have OpenNETCF installed/referenced, and use "using OpenNETCF.Win32;"

 
精彩推荐
图片推荐