3

I have been searching for the last 2 hours, and I actualy just have been searching stupid.

I am trying to read a Registry_binary Value and convert this to a string. I tried several things I've found online(includeing some stackoverflow posts), but seems I cannot get it work:

class Class1 {
    RegistryKey RegKey;
    String keys;

    static void Main() {
        Class1 c=new Class1();
        c.initialize();
    }

    void initialize() {
        RegKey=Registry.LocalMachine.OpenSubKey("the location", true);
        var bytearray=Converter<RegKey.GetValue("key"), String[keys]>;
        Console.WriteLine(bytearray);
        System.Threading.Thread.Sleep(5000);
    }
}

I also tried to use:

keys=keys+BitConverter.ToString(System.byte[RegKey.GetValue("key")]);

On request:

RegKey=Registry.LocalMachine.OpenSubKey("Software\\MXstudios\\riseingtesharts", true);
keys=RegKey.GetValue("key");

and this will output System.Bytes[]

Ken Kin
  • 4,503
  • 3
  • 38
  • 76
MX D
  • 2,453
  • 4
  • 35
  • 47
  • How do you want to convert it to a string? What kind of string do you want to get? – SLaks Feb 03 '13 at 00:22
  • @SLaks i wish to convert the registry_binary ( which got a hex value inside) to a normal text string, if possible in the hex format – MX D Feb 03 '13 at 00:23
  • Can you give us an example of registry_binary and the expected resulting string? – deej Feb 03 '13 at 00:29
  • @deej Added it under the tag On request . i would like to have the output as it is in the registry which in this case would be : 84-F6-61-B0-06-E5-55-FF-36 – MX D Feb 03 '13 at 00:34

1 Answers1

6

Assuming the key is opened

var valueName = "some binary registry value";
var valueKind = registryKey.GetValueKind(valueName);
if (valueKind == RegistryValueKind.Binary)
{
    var value = (byte[])registryKey.GetValue(valueName);
    var valueAsString = BitConverter.ToString(value);
}

EDIT: some explanations:

GetValue returns object, and BitConverter.ToString gets an array of bytes as an argument. Thus we cast the value returned by GetValue to byte[], to be able to use it within BitConverter.ToString. But first we check if the registry value is actually binary. Then you can cast it to byte[] safely, because the object returned by GetValue for binary values is actually byte array.

deej
  • 1,703
  • 1
  • 24
  • 26