我怎样才能从registry读取二进制数据到字节数组

我使用以下代码将一个字节数组保存到registry

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

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

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

现在我想把相同的数据读回字节数组格式。 以下代码可以读取相同的数据,但输出是types对象。

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

这里转换为byte []不起作用。 我知道我可以使用串行器或stream来实现这个任务。 我想知道是否有一个更简单的方法来读取数据回到byte []types(双线性代码)?

请注意这个问题是在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是您的密钥位置的根名称,并指向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);