Read Registry_binary and convert to string

限于喜欢 提交于 2019-12-19 04:07:53

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/14668091/read-registry-binary-and-convert-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!