示例#1
0
        // This function takes a registry key and recursively enumerates its values
        // and subkeys while adding each to the parent TreeNode for listing
        // in the TreeView control.
        private void PopulateTreeFromReg(string rootKey, string keyName, ref TreeNode parent)
        {
            // make sure rootKey and keyName aren't empty
            if (!rootKey.Equals(""))
            {
                // if keyName is not empty create new node for this key
                // otherwise its the root key so just use parent
                TreeNode newNode;
                bool     Named; // true if it's the root node
                if (!keyName.Equals(""))
                {
                    newNode = new TreeNode(keyName);
                    Named   = false;
                }
                else
                {
                    // current key is the root node, so use parent
                    // (the passed in root node) as the current node
                    newNode = parent;
                    Named   = true;
                }

                // list the values first
                string Values = Registry.EnumValues(Registry.RootKey.LocalMachine, rootKey, false);

                // make sure Values is not null
                if (Values != null)
                {
                    // split the coma-delimited return string up into the individual values
                    string[] ValList = Values.Split(',');
                    // iterate through each value and add to the tree
                    foreach (string curVal in ValList)
                    {
                        if (!curVal.Equals(""))
                        {
                            TreeNode newVal = new TreeNode(curVal);
                            newNode.Nodes.Add(newVal);
                        }
                    }
                }

                // now list the keys
                string Keys = Registry.EnumValues(Registry.RootKey.LocalMachine, rootKey, true);
                if (Keys != null)
                {
                    // split up the coma-delimited key list
                    string[] KeyList = Keys.Split(',');
                    // iterate through each subkey and add to the tree
                    foreach (string curKey in KeyList)
                    {
                        if (!curKey.Equals(""))
                        {
                            string subKey = rootKey + "\\" + curKey;
                            PopulateTreeFromReg(subKey, curKey, ref newNode);
                        }
                    }
                }
                // now add the node to the parent
                if (!Named)
                {
                    parent.Nodes.Add(newNode);
                }
            }
        }