[英]How can I read binary data from registry to byte array
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
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.
以下代码可以读取相同的数据,但输出的类型为object。
RegistryKey key = Registry.CurrentUser.OpenSubKey(KeyName);
object obj = key.GetValue(@"Software\Software\Key", value);
Here casting to byte[] does not work. 这里转换为byte []不起作用。 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)?
我想知道是否有更简单的方法将数据读回byte []类型(双线代码)?
Please note this question is in C++ 请注意这个问题是用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);
To Retrieve the data back from registry into Byte[] format use following: 要将数据从注册表检索回Byte []格式,请使用以下命令:
RegistryKey key = Registry.CurrentUser.OpenSubKey(KeyName);
byte[] Data = (byte[]) key.GetValue(@"Software\AppName\Key", value);
Note: CurrentUser
is name of the root for your Key location and points to HKEY_CURRENT_USER
注意:
CurrentUser
是Key位置的根名称,并指向HKEY_CURRENT_USER
I test in VB.NET: 我在VB.NET中测试:
Dim obj As Object = key.GetValue("Software\Software\Key", value__1)`
Dim v As [Byte]() = CType(obj, Byte())`
and it works 它的工作原理
so in C# should be: 所以在C#应该是:
Byte[] v = Convert.ToByte(obj);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.