c#如何将treeNode添加到treeView而不重复

时间:2014-03-07 22:03:51

标签: c# treeview

当我运行下面的脚本时,我的treeView上没有显示任何内容。我想我错过了一些东西。所有变量都已定义

        if (files != null)
        {
            int check = 0;
            string[] array= null;
            foreach (System.IO.FileInfo file in files)
            {
                if (treeView1.Nodes.Count > 0)
                {
                    for (int i = 0; i <= treeView1.Nodes.Count; i++)
                    {
                        array[i] = treeView1.Nodes[i].Name;
                    }
                    for (int m = 0; m <= treeView1.Nodes.Count; m++)
                    {
                        if (array[m] == file.DirectoryName)
                        {
                            check++;
                        }
                    }
                    if (check == 0)
                    {
                        treeView1.Nodes.Add(file.DirectoryName);
                    }
                }
            }
         }

2 个答案:

答案 0 :(得分:0)

这可能是我用于快速肮脏原型设计的一些代码。希望它会让你开始。

List<DirectoryInfo> dirs = Directory.GetDirectories(@"C:\windows").Select(d => new DirectoryInfo(d)).ToList();    
List<FileInfo> files = Directory.GetFiles(@"C:\windows", "*.*").Select(f => new FileInfo(f)).ToList(); 
if (dirs != null) treeView1.Nodes.AddRange(dirs.Select(d => new TreeNode() { Text = d.FullName }).ToArray());
if (files != null) treeView1.Nodes.AddRange(files.Select(f => new TreeNode() { Text = f.FullName }).ToArray());

答案 1 :(得分:0)

我得到了问题的答案!让所有事情都得到恰当的展示。

    private void Form1_Load(object sender, EventArgs e)
    {
        System.IO.DirectoryInfo Root = new System.IO.DirectoryInfo(@"C:\");
        // treeView1.Nodes.Add("Local Disk");
        WalkDirectoryTree(Root);

        treeView1.PathSeparator = @"\";
        PopulateTreeView(treeView1, paths, '\\');


        DriveInfo[] ListDrives = DriveInfo.GetDrives();
        foreach (DriveInfo Drive in ListDrives)
        {
            if (Drive.DriveType == DriveType.Removable)
           {
                if (Drive.IsReady == true)
                {
                    TreeNode node = new TreeNode(Drive.Name);
                    node.Tag = Drive;
                    WalkDirectoryTree(Drive.RootDirectory);

                    treeView2.PathSeparator = @"\";
                    PopulateTreeView(treeView2, paths, '\\');
                }
            }
        }
    }

    //**************************************************************************************************

    List<string> paths = new List<string>();
    private void FullPath(string str)
    {
        paths.Add(str);
    }
    //**************************************************************************************************

    void WalkDirectoryTree(System.IO.DirectoryInfo root)
    {
        System.IO.FileInfo[] files = null;
        System.IO.DirectoryInfo[] subDirs = null;

        // First, process all the files directly under this folder 
        try
        {
            files = root.GetFiles("*.lst");
        }
        catch (UnauthorizedAccessException e)
        {
            log.Add(e.Message);
        }
        catch (System.IO.DirectoryNotFoundException e)
        {
            Console.WriteLine(e.Message);
        }
        if (files != null)
        {
            foreach (System.IO.FileInfo fi in files)
            {

                FullPath(fi.DirectoryName);
            }

            subDirs = root.GetDirectories();

            foreach (System.IO.DirectoryInfo dirInfo in subDirs)
            {
                WalkDirectoryTree(dirInfo);
            }
        }
    }
    //**************************************************************************************************


    private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
    {
        TreeNode lastNode = null;
        string subPathAgg;
        foreach (string path in paths)
        {
            subPathAgg = string.Empty;
            foreach (string subPath in path.Split(pathSeparator))
            {
                subPathAgg += subPath + pathSeparator;
                TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
                if (nodes.Length == 0)
                    if (lastNode == null)
                        lastNode = treeView.Nodes.Add(subPathAgg, subPath);
                    else
                        lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
                else
                    lastNode = nodes[0];
            }
            lastNode = null;
        }
    }

}