简体   繁体   English

如何在 Windows CE 中从注册表中读取和写入值?

[英]How to read and write value from registry in Windows CE?

如何将值1插入到HKEY_LOCAL_MACHINE\\SOFTWARE\\WJST\\WLAN的属性CDInsert并读取回来?

http://msdn.microsoft.com/en-us/library/microsoft.win32.registry%28v=VS.90%29.aspx http://msdn.microsoft.com/en-us/library/microsoft.win32.registry%28v=VS.90%29.aspx

Try this:试试这个:

//using Microsoft.Win32;

RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WJST\WLAN", true); 

// set value of "CDInsert" to 1
reg.SetValue("CDInsert", 1, RegistryValueKind.DWord);

// get value of "CDInsert"; return 0 if value not found
int value = (int)reg.GetValue("CDInsert", 0);

Sometimes if you try to access the key directly with key.SetValue() throws an exception because you don't have the rights.有时,如果您尝试使用key.SetValue()直接访问密钥, key.SetValue()抛出异常,因为您没有权限。

You can use Registry.SetValue() like in the documentation or like following example:您可以像文档中一样使用Registry.SetValue()或像以下示例一样:

const string root = "HKEY_LOCAL_MACHINE"; // Root of your path
const string path = @"\SOFTWARE\WJST\WLAN"; // Path of your key
const string default = "CDInsert"; // Name of the key
const string value = "1";

Registry.SetValue(root + "\\" + path, default, value);

Pay attention if your registry is volatile than you should flush and persist:请注意您的注册表是否比您应该刷新并持久化的不稳定:

const UInt32 HKEY_LOCAL_MACHINE = 0x80000002;

IntPtr hkeyLocalMachine = new IntPtr(unchecked((int)HKEY_LOCAL_MACHINE ));
RegFlushKey(hkeyLocalMachine);

For use RegFlushKey you need to add an extern method:要使用RegFlushKey您需要添加一个 extern 方法:

[DllImport("coredll.dll")]
public static extern int RegFlushKey(IntPtr hKey);

Usefull links: pinvoke.net , regflushkey .有用的链接: pinvoke.netregflushkey

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM