Question

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[]

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top