Read Registry_binary and convert to string

▼魔方 西西 提交于 2019-11-30 23:41:47

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.

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