How can I read binary data from the registry into a byte array

I saved an array of bytes in the registry using the 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 the 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 the byte array format. The following code can read the same data, but the output has an object of type.

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

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

Please note that this question is in C ++

+6
source share
2 answers

To write an array of bytes to the registry, use the 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 data from the registry in Byte [] format, use the following:

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

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

+5
source

I am testing in VB.NET:

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

and he works

therefore in C # should be:

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

All Articles