使用P / Invoke写入注册表

时间:2011-05-06 07:01:16

标签: c# pinvoke windows-ce

我需要使用c#和P / Invoke更改windowsCE中的注册值(RapiDll不在那里)

我知道如何阅读密钥:

private static string ReadRegKey(UIntPtr rootKey, string keyPath, string valueName,string value)
    {
        IntPtr hKey = IntPtr.Zero;
        if (RegOpenKeyEx(rootKey, keyPath, 0, KEY_READ, out hKey) == 0)
        {
            uint size = 1024;
            uint type = 0;
            string keyValue = null;
            StringBuilder keyBuffer = new StringBuilder();
            keyBuffer.Append(value);

            if (RegQueryValueEx(hKey, valueName, IntPtr.Zero, ref type, keyBuffer, ref size) == 0)
                keyValue = keyBuffer.ToString();

            RegCloseKey(hKey);

            return (keyValue);
        }

        return (null);  // Return null if the value could not be read
    }

任何人都可以帮我吗? (这是用于更改设备名称btw)

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

为什么不想使用Microsoft.Win32名称空间中的RegistryKey类?

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

答案 2 :(得分:0)

如果您打算编写/读取/查询不在wow6432节点内的注册表值,并且如果您使用的框架小于4.0,则需要RegistryEX类型的P / Invoke dll

例如,您在64位应用程序上运行32位应用程序,由于虚拟化,注册表将在32位节点下。如果您需要在64位下创建它,那么您需要使用这些p / invokes

希望并祝你成功