我正在尝试用字符串形式的注册表中的所有内容填充树结构,以便以后可以使用它来比较更改。到目前为止,我已经陷入僵局,实际上是向树结构中动态添加子项,因为我永远无法确定注册表中的子项将拥有多少个子项!或者它会有多少值,所以我必须能够动态地填充树结构而不知道会有多少孩子。
到目前为止,我有这个:
private void buildRegistryListTreeFromScratch(string[] rootSubs) {
//The rootSubs argument is the first layer of subkeys directly underneath the root keys
RegistryKey rootKey = Registry.CurrentUser;
registryTreeTracker.Name = "Current User";
for (int i = 0; i < rootSubs.Length; i++) {
registryTreeTracker.Children.Add(new RegistryTreeNode(rootSubs[i]));
}
for (int i = 0; i < rootSubs.Length; i++) {
RegistryKey selectedKey = rootKey.OpenSubKey(rootSubs[i]);
if (selectedKey.SubKeyCount > 0) {
subKeyBelow(rootSubs[i]);
}
}
}
private void subKeyBelow(string path) {
RegistryKey rootKey = Registry.CurrentUser;
RegistryKey selectedKey = rootKey.OpenSubKey(path);
for (int i = 0; i < registryTreeTracker.Children.Count; i++){
if (registryTreeTracker.Children[i].Name == path) {
registryTreeTracker.Children[i] ... //This is the impasse I have reached
}
}
}
我用于对象 registryTreeTracker 的类是 Daniel Hilgarth 在与此相关的问题中提供给我的类,其定义如下:
public class RegistryTreeNode {
private readonly List<RegistryTreeNode> _children = new List<RegistryTreeNode>();
public RegistryTreeNode(string name, params RegistryTreeNode[] children) {
Name = name;
_children.AddRange(children);
}
public List<RegistryTreeNode> Children {
get {
return _children;
}
}
public string Name {
get;
set;
}
}