首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >C# treeView是将子节点排列为其父节点的最后一个子节点的代码

C# treeView是将子节点排列为其父节点的最后一个子节点的代码
EN

Stack Overflow用户
提问于 2019-04-19 23:43:35
回答 2查看 142关注 0票数 0

我使用treeView在表单上的treeView中显示序列目录及其子目录和文件,并使用以下方法加载树视图

在表单加载中:

代码语言:javascript
复制
        treeView1.Nodes.Clear();
        toolTip1.ShowAlways = true;
        LoadDirectory("C:\\Windows\\System32\\" + inventedName );  

以及以下3种加载目录、子目录和文件的方法

代码语言:javascript
复制
    public void LoadDirectory(string Dir)
    {

        DirectoryInfo di = new DirectoryInfo(Dir);

        TreeNode tds = treeView1.Nodes.Add(di.Name);

        tds.Tag = di.FullName;
        //tds.StateImageIndex = 0;
        tds.ImageIndex = 0;
        tds.StateImageIndex = 0;
        tds.SelectedImageIndex = 0;
        LoadFiles(Dir, tds);
        LoadSubDirectories(Dir, tds);
    }

    private void LoadSubDirectories(string dir, TreeNode td)
    {

        string[] subdirectoryEntries = Directory.GetDirectories(dir);          

        // Loop through them to see if they have any other subdirectories  
        foreach (string subdirectory in subdirectoryEntries)
        {

            DirectoryInfo di = new DirectoryInfo(subdirectory);
            TreeNode tds = td.Nodes.Add(di.Name);
            renameNodes(tds);    
            //tds.StateImageIndex = 0;
            tds.Tag = di.FullName;
            tds.ImageIndex = 0;
            tds.StateImageIndex = 0;
            tds.SelectedImageIndex = 0;
            LoadFiles(subdirectory, tds);
            LoadSubDirectories(subdirectory, tds);

        }
    }

    private void LoadFiles(string dir, TreeNode td)
    {
        string[] Files = Directory.GetFiles(dir, "*.pdf");

        // Loop through them to see files  
        foreach (string file in Files)
        {
            FileInfo fi = new FileInfo(file);
            TreeNode tds = td.Nodes.Add(fi.Name);
            tds.Tag = fi.FullName;
            tds.ImageIndex = 1;
            tds.StateImageIndex = 1;
            tds.SelectedImageIndex = 1;

        }
    }

我的问题是子目录(文件夹)有特定的名称,我不能更改它,例如:

代码语言:javascript
复制
> root 
    > parent 
          > 1.0 xxx
          > 1.10 xxx
          > 1.2 xxx
          > 1.3 xxx 
          > 1.4 xxx
          > 1.5 xxx
          > 1.6 xxx
          > 1.7 xxx
          > 1.8 xxx
          > 1.9 xxx

但我需要它是这样的

代码语言:javascript
复制
> root 
    > parent 
         > 1.0 xxx
         > 1.2 xxx
         > 1.3 xxx 
         > 1.4 xxx
         > 1.5 xxx
         > 1.6 xxx
         > 1.7 xxx
         > 1.8 xxx
         > 1.9 xxx
         > 1.10 xxx 

愚蠢的(1.10xxx)子文件夹必须在(1.9xxx)子文件夹之后,并且正如我所说的,我不能重命名文件夹,这将是错误的,有什么方法可以将其发送到最后一个子文件夹

谢谢你帮我

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55764366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档