我正在制作像窗口regedit这样的程序。
我使用C#来获取注册表项和值。
我有问题获得一些注册表值。当我得到HKLM \ SYSTEM \ ControlSet001 \ Arbiters \ AlocationOrder的值。
有Root值。值类型为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;
}
}
}
这是我获取注册表值的代码的一部分。它可以获得有价值的名称 string [] valueNames = rootKey.GetValueNames(); 但是当我试图获得注册表的值时,它们返回null并且值类型未知。
如何使用c#获取这样的注册表值?
答案 0 :(得分:0)
尝试以下代码
Dim SettingType = regSubKey.GetValueKind(SettingName)
If SettingType = RegistryValueKind.Binary Then
type = "REG_BINARY"
Dim value = regSubKey.GetValue(SettingName)
SettingValue = BitConverter.ToString(value)
end if