0

我正在尝试用字符串形式的注册表中的所有内容填充树结构,以便以后可以使用它来比较更改。到目前为止,我已经陷入僵局,实际上是向树结构中动态添加子项,因为我永远无法确定注册表中的子项将拥有多少个子项!或者它会有多少值,所以我必须能够动态地填充树结构而不知道会有多少孩子。

到目前为止,我有这个:

    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;
    }
}
4

2 回答 2

1

您将使用递归方法来构建您的树。此方法将接收树的当前级别的根键以及应添加到该根键的注册表键。
它看起来像这样:

public TreeNode AddChildNode(TreeNode parent, string name)
{
    var child = new TreeNode(name);
    parent.Children.Add(child);
    return child;
}

public void AddSubTree(TreeNode treeRoot, RegistryKey registryKeyToAdd)
{
    var child = AddChildNode(treeRoot, registryKeyToAdd.Name);

    foreach(var subKeyName in registryKeyToAdd.GetSubKeyNames())
    {
        try
        {
            AddSubTree(child, registryKeyToAdd.OpenSubKey(subKeyName));
        }
        catch(SecurityException e)
        {
            AddChildNode(child, string.Format("{0} - Access denied", subKeyName));
        }
    }
}

public TreeNode BuildRegistryTree()
{
    var root = new TreeNode("Computer");
    AddSubTree(root, Registry.CurrentUser);
    AddSubTree(root, Registry.LocalMachine);
    return root;
}

你会打电话BuildRegistryTree来获取完整的树。

于 2013-07-10T17:05:46.073 回答
0

为了更明确的问题,感谢 Daniel Hilgarth,这是我工作的高潮:

    //the rootSubs argument is a string array containing all the subkeys of the root keys
    private void buildRegistryListTreeFromScratch(string[] rootSubs) {

        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) {
                recursiveSubKeyBelow(registryTreeTracker.Children[i], path);
            }
        }
    }

    private void recursiveSubKeyBelow(RegistryTreeNode currentNode, string path) {
        RegistryKey rootKey = Registry.CurrentUser;
        RegistryKey selectedKey = rootKey.OpenSubKey(path);
        string originalPath = path;
        RegistryKey originalKey = rootKey.OpenSubKey(originalPath);

        for (int i = 0; i < selectedKey.SubKeyCount; i++){
        currentNode.Children.Add(new RegistryTreeNode(selectedKey.GetSubKeyNames()[i]));
        }

        for (int i = 0; i < originalKey.SubKeyCount; i++) {
            string deeperPath = originalPath + "\\" + originalKey.GetSubKeyNames()[i];
            try {
                selectedKey = rootKey.OpenSubKey(deeperPath);
                if (selectedKey.SubKeyCount > 0) {
                    recursiveSubKeyBelow(currentNode.Children[i], deeperPath);
                }
            } catch(SecurityException) {
                i++;
            }
        }
    }

感谢负载丹!有了您的建议/帮助,这个问题变得更加易于管理!

于 2013-07-11T14:14:15.187 回答