我正在制作像窗口registry一样的程序。
我使用C#来获取registry项和值。
我有问题得到一些registry值。 当我得到HKLM \ SYSTEM \ ControlSet001 \ Arbiters \ AlocationOrder的价值。
有根值。 值types是REG_RESOURCE_REQUIREMENT_LIST。 当我在c#中使用RegistryKey.getValue得到这个值时,它返回null。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Win32; using System.Security.AccessControl; using System.Security; namespace GetRegistryList { class RegData { public string name { get; set; } public string fullPath { get; set; } public string regRoot { get; set; } public Boolean hasFolder { get; set; } public List<RegValue> values { get; set; } public RegData() { this.values = new List<RegValue>(); } public void setRegDataValue(string[] args, RegData regData) { string rootString = ""; string fullPath = ""; if (args.Length > 1) { rootString = args[0]; fullPath = args[1]; } **//HKLM fullPath = SYSTEM\ControlSet002\Control\Arbiters\AllocationOrder\ RegistryKey rootKey = getRootKey(rootString).OpenSubKey(fullPath, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey);** string[] splitResult; if (fullPath == "\\") { regData.name = fullPath; } else { splitResult = fullPath.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries); regData.name = splitResult[splitResult.Length - 1]; } regData.fullPath = fullPath; regData.regRoot = rootString; regData.hasFolder = rootKey.SubKeyCount > 0 ? true : false; //this.regData.values = string[] valueNames = rootKey.GetValueNames(); foreach (var name in valueNames) { //val RegistryValueKind valuekind = rootKey.GetValueKind(name); RegValue tempReg; Object a = rootKey.GetValue(name); //Console.WriteLine(); switch (valuekind) { case RegistryValueKind.Binary: System.Byte[] arr = (System.Byte[])rootKey.GetValue(name); tempReg = new RegValue(name, "Binary", System.Text.Encoding.UTF8.GetString(arr)); break; case RegistryValueKind.DWord: tempReg = new RegValue(name, "DWord", rootKey.GetValue(name).ToString()); break; case RegistryValueKind.ExpandString: tempReg = new RegValue(name, "ExpandString", rootKey.GetValue(name).ToString()); break; case RegistryValueKind.QWord: tempReg = new RegValue(name, "QWord", rootKey.GetValue(name).ToString()); break; case RegistryValueKind.String: tempReg = new RegValue(name, "String", rootKey.GetValue(name).ToString()); break; case RegistryValueKind.MultiString: string str = ""; foreach (var v in (System.String[])rootKey.GetValue(name)) { str += v + System.Environment.NewLine; } tempReg = new RegValue(name, "MultiString", str); break; case RegistryValueKind.None: tempReg = new RegValue(name, "None", rootKey.GetValue(name).ToString()); break; **case RegistryValueKind.Unknown: // value type is unkown and rootKey.getValue(name) is null.** tempReg = new RegValue(name, "Unknown", rootKey.GetValue(name) as string); break; default: tempReg = new RegValue(name, "NoDefine", rootKey.GetValue(name).ToString()); break; } regData.values.Add(tempReg); }//foreach } public RegistryKey getRootKey(string rootString) { switch (rootString) { case "HKCR": return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.ClassesRoot, RegistryView.Registry64); case "HKCU": return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry64); case "HKLM": return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64); case "HKUS": return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.Users, RegistryView.Registry64); case "HKCC": return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentConfig, RegistryView.Registry64); case "HKDD": return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.DynData, RegistryView.Registry64); case "HKPD": return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.PerformanceData, RegistryView.Registry64); default: return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.ClassesRoot, RegistryView.Registry64); break; } } } public class RegValue { public string valueName { get; set; } public string type { get; set; } public string stringValue { get; set; } public System.Byte[] binaryValue; public RegValue(string valueName, string type, string stringValue) { this.valueName = valueName; this.type = type; this.stringValue = stringValue; } public RegValue(string valueName, string type, System.Byte[] binaryValue) { this.valueName = valueName; this.type = type; this.binaryValue = binaryValue; } } }
这是我的代码获取registry值的一部分。 它可以在string []中获取valuenames valueNames = rootKey.GetValueNames(); 但是当我尝试获得registry的价值,他们返回null和值types是未知的。
我怎样才能得到像这样使用c#registry的价值?