C# 从注册表中的路径递归填充树视图

C# Recursivly populate treeview from paths in registry

问题:如何使用 C# 将注册表中的多个路径值作为字符串递归填充到树视图节点中?

我想做什么: 用我的 list[] 数组中收集的每个注册表值的字符串值递归地填充我的 treeView1 也用于 listBox (listBox1) 并且当然只添加非 null 的注册表中的值。非常感谢您对此问题的任何帮助或意见。

我如何将路径加载到 treeView1

private void PopulateTreeView()
        {
            try
            {
                TreeNode rootNode;
                NodeInfo nInfo;

                string path = Global.GetStartUpPath();
                DirectoryInfo info = new DirectoryInfo(path);
                if (info.Exists)
                {
                    rootNode = new TreeNode(info.Name, 3, 3);
                    rootNode.Name = info.Name;

                    nInfo = new NodeInfo(NodeInfo.Types.Root, info.FullName);
                    rootNode.Tag = nInfo;

                    GetDirectories(info, rootNode);
                    treeView1.Nodes.Add(rootNode);
                    treeView1.SelectedNode = rootNode;
                }

            }

我如何设置路径:

public static void setStartUpPath(string path)
        {
            //Open the registry key
            RegistryKey rk1 = Registry.CurrentUser.OpenSubKey(@"Software", true);
            //Create a subkey, if not exist
            RegistryKey sk1 = rk1.CreateSubKey(@"Example\Test\Path");

            sk1.SetValue("StartupPath", path);

            sk1.Close();

            _startUpPath = path;
        }

我是怎么得到的:

public static string GetStartUpPath()
    {
        if ( (_startUpPath != null) && (_startUpPath != string.Empty) )
                return _startUpPath;           
        //Check registry
        else
        {
            //Open the registry key
            RegistryKey rk1 = Registry.CurrentUser.OpenSubKey(@"Software\Example\Test\Path", true);
            if (rk1 == null)
                return string.Empty;

            _startUpPath = (string)rk1.GetValue("StartupPath");
            rk1.Close();
            return _startUpPath;
        }


    }

(如果有帮助)我获取列表框路径的方法

public static string[] GetPaths()
        {
            //Open the registry key
            RegistryKey rk1 = Registry.CurrentUser.OpenSubKey(@"Software\Example\Test\Path\List", true);
            if (rk1 == null)
                return null;

            string[] list = new string[rk1.ValueCount];


            for (int i = 0; i < rk1.ValueCount; i++)
            {
                list[i] = rk1.GetValue(i.ToString()).ToString();
            }
            rk1.Close();
            return list;

        }

自己解决了。对于来这里寻找答案的其他人,这就是我解决树视图的方法。

        try
        {
            TreeNode rootNode;
            NodeInfo nInfo;

            string[] paths = Global.GetPaths();
            for (int i = 0; i < paths.Length; i++)
            {
                string path = paths[i];
                DirectoryInfo info = new DirectoryInfo(path);
                if (info.Exists)
                {
                    rootNode = new TreeNode(info.Name, 3, 3);
                    rootNode.Name = info.Name;

                    nInfo = new NodeInfo(NodeInfo.Types.Root, info.FullName);
                    rootNode.Tag = nInfo;

                    GetDirectories(info, rootNode);
                    treeView1.Nodes.Add(rootNode);
                    treeView1.SelectedNode = rootNode;
                }
            }

        }

        catch (Exception ex)
        {
            //.....
            Logic.Log.write("ERROR PopulateTreeView -" + ex.Message);
        }