如何从注册表读取二进制数据到字节数组 [英] How can I read binary data from registry to byte array

查看:54
本文介绍了如何从注册表读取二进制数据到字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I saved a byte array to registry using following code

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

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

Here is the key created using above code:

[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类型.

Now I want to read the same data back to byte array format. Following code can read the same data but the output is of type object.

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

此处强制转换为byte []不起作用.我知道我可以使用序列化器或流来完成此任务.我想知道是否有一种更简单的方法可以将数据读回byte []类型(两个线性代码)?

Here casting to byte[] does not work. I know I can use serializer or streams to achieve this task. I would like to know if there is an easier way to read data back to byte[] type (A two liner code)?

请注意此问题使用C ++

推荐答案

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

To write a byte array to registry use following code

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 []格式,请使用以下命令:

To Retrieve the data back from registry into Byte[] format use following:

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

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

Note: CurrentUser is name of the root for your Key location and points to HKEY_CURRENT_USER

这篇关于如何从注册表读取二进制数据到字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆