如需要讀取注冊表中某個鍵的值,
例如讀取DriverDesc對應的值,一般情況下為String類型,讀取代碼如下:
RegistryKey driverKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Class\{4D36E968-E325-11CE-BFC1-08002BE10318}\0000"); string result = (String)driverKey.GetValue("DriverDesc");
但有時候值的類型為REG_BINARY(二進制)類型,此時需要將值按字節數組的方式讀取出來,再將字節數組轉換成最終的字符串結果:
RegistryKey driverKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Class\{4D36E968-E325-11CE-BFC1-08002BE10318}\0000");//按路徑打開注冊表 byte[] array = (byte[])driverKey.GetValue("DriverDesc");//獲取DriverDesc值的字節數組 string decoded = System.Text.Encoding.UTF8.GetString(array);//將字節數組轉換成字符串 decoded = decoded.Replace("\0", String.Empty);//由於將字節數組轉換成字符串的過程中,一般會包含\0字符,所以要將它替換成空字符串,否則顯示會有問題
