繁体   English   中英

如何从注册表到字节数组读取二进制数据

[英]How can I read binary data from registry to byte array

我使用以下代码将一个字节数组保存到注册表中

Byte[] value = new byte[16]{
    0x4a,0x03,0x00,0x00, 
    0x45,0x02,0x00,0x00, 
    0xb7,0x00,0x00,0x00, 
    0x9d,0x00,0x00,0x00
};

RegistryKey key = Registry.CurrentUser.CreateSubKey(KeyName);
key.SetValue(@"Software\Software\Key", value, RegistryValueKind.Binary);

这是使用上面代码创建的密钥:

[HKEY_CURRENT_USER\Software\Software\Key]  
    "LOC"=hex:4a,03,00,00,45,02,00,00,b7,00,00,00,9d,00,00,00

现在我想将相同的数据读回字节数组格式。 以下代码可以读取相同的数据,但输出的类型为object。

RegistryKey key = Registry.CurrentUser.OpenSubKey(KeyName);
object obj =  key.GetValue(@"Software\Software\Key", value);

这里转换为byte []不起作用。 我知道我可以使用序列化程序或流来完成这项任务。 我想知道是否有更简单的方法将数据读回byte []类型(双线代码)?

请注意这个问题是用C ++编写的

要将一个字节数组写入注册表,请使用以下代码

Byte[] value = new byte[]{
    0x4a,0x03,0x00,0x00, 
    0x45,0x02,0x00,0x00, 
    0xb7,0x00,0x00,0x00, 
    0x9d,0x00,0x00,0x00
};

RegistryKey key = Registry.CurrentUser.CreateSubKey(KeyName);
key.SetValue(@"Software\AppName\Key", value, RegistryValueKind.Binary);

要将数据从注册表检索回Byte []格式,请使用以下命令:

RegistryKey key = Registry.CurrentUser.OpenSubKey(KeyName);
byte[] Data =  (byte[]) key.GetValue(@"Software\AppName\Key", value);

注意: CurrentUser是Key位置的根名称,并指向HKEY_CURRENT_USER

我在VB.NET中测试:

Dim obj As Object = key.GetValue("Software\Software\Key", value__1)`
Dim v As [Byte]() = CType(obj, Byte())`

它的工作原理

所以在C#应该是:

Byte[] v = Convert.ToByte(obj);

暂无
暂无

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

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