读取Registry_binary并转换为字符串

我一直在寻找最近2个小时,而我实际上一直在寻找愚蠢的。

我试图读取Registry_binary值并将其转换为字符串。 我尝试了一些我在网上找到的东西(包括一些stackoverflowpost),但似乎我无法让它工作:

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; Console.WriteLine(bytearray); System.Threading.Thread.Sleep(5000); } } 

我也试过用:

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

根据要求:

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

这将输出System.Bytes[]

假设钥匙被打开了

 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); } 

编辑:一些解释:

GetValue返回对象,BitConverter.ToString获取一个字节数组作为参数。 因此,我们将GetValue返回的值转换为byte [],以便能够在BitConverter.ToString中使用它。 但首先我们检查注册表值是否实际为二进制。 然后你可以安全地将它转换为byte [],因为GetValue为二进制值返回的对象实际上是字节数组。