C#读取注册表中二进制类型的值(REG_BINARY)

如需要读取注册表中某个键的值,

  例如读取DriverDesc对应的值,一般情况下为String类型,读取代码如下:

RegistryKey driverKey = Registry.LocalMachine.OpenSubKey(@"SystemCurrentControlSetControlClass{4D36E968-E325-11CE-BFC1-08002BE10318}000");
string result = (String)driverKey.GetValue("DriverDesc");

但有时候值的类型为REG_BINARY(二进制)类型,此时需要将值按字节数组的方式读取出来,再将字节数组转换成最终的字符串结果:

RegistryKey driverKey = Registry.LocalMachine.OpenSubKey(@"SystemCurrentControlSetControlClass{4D36E968-E325-11CE-BFC1-08002BE10318}000");//按路径打开注册表
byte[] array = (byte[])driverKey.GetValue("DriverDesc");//获取DriverDesc值的字节数组
string decoded = System.Text.Encoding.UTF8.GetString(array);//将字节数组转换成字符串
decoded = decoded.Replace("", String.Empty);//由于将字节数组转换成字符串的过程中,一般会包含字符,所以要将它替换成空字符串,否则显示会有问题
原文地址:https://www.cnblogs.com/tommy-huang/p/6770057.html