动态调整用注册表值填充的树 [英] Dynamicly Adjusting Tree populated with registry values

查看:45
本文介绍了动态调整用注册表值填充的树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用字符串形式的注册表中的所有内容填充树结构,以便以后可以使用它与更改进行比较.到目前为止,我无法为树形结构提供动态的子级添加功能而陷入僵局,因为我无法确定注册表中一个子项将拥有多少个子级!或它将具有多少个值,因此我必须能够动态填充树结构,而无需知道将有多少个子代.

I am attempting to populate a tree structure with everything from the registry in string form so that I may use it to make comparisons to changes later on. So far I've come to an impasse with actually providing dynamic additions of children to the tree structure as I can never be certain as to how many children a subkey in the registry is going to have! Or how many values it will have, so I have to be able to dynamically populate the tree structure without knowing how many children there will ever be.

到目前为止,我有这个:

So far I have this:

    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在与此相关的问题中提供给我的类,其定义如下:

the class I am using for the object registryTreeTracker is a class provided to me by Daniel Hilgarth in a question related to this, and its definition is as follows:

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

推荐答案

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

You would use a recursive method to build your tree. This method would receive the root key of the current level of the tree as well as the registry key that should be added to that root key.
It would look something like this:

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 以获得完整的树.

You would call BuildRegistryTree to get the complete tree.

这篇关于动态调整用注册表值填充的树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆