首页
学习
活动
专区
工具
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

回答 2

Stack Overflow用户

发布于 2019-04-20 01:05:22

几周前,我使用IEquable做了一个非常类似的解决方案。我在下面的代码中对文件名进行了排序,以获得正确的解决方案

代码语言:javascript
复制
   public class Test
    {
        private void LoadFiles(string dir, TreeNode td)
        {
            string[] Files = Directory.GetFiles(dir, "*.pdf");
            Files = Files.Select(x => new MySort(x)).OrderBy(x => x).Select(x => x.filename).ToArray();

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

            }
        }
    }
    public class MySort : IComparable
    {
        private string[] splitvalues { get; set; }
        public string filename { get; set; }

        public MySort(string filename)
        {
            this.filename = filename;
            splitvalues = filename.Split(new char[] { '.', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

        }
        public int CompareTo(object other)
        {
            MySort otherMySort = (MySort)other;
            int min = Math.Min(this.splitvalues.Length, otherMySort.splitvalues.Length);

            for (int i = 0; i < min; i++)
            {
                string a = this.splitvalues[i];
                string b = otherMySort.splitvalues[i];

                if (a != b)
                {

                    int numberA = 0;
                    int numberB = 0;

                    if (int.TryParse(a, out numberA))
                    {
                        if (int.TryParse(b, out numberB))
                        {
                            int z = numberA.CompareTo(numberB);
                            //a & b are numbers
                            return numberA.CompareTo(numberB);
                        }
                        else
                        {
                            //a number b string
                            return -1;
                        }

                    }
                    else
                    {
                        if (int.TryParse(b, out numberB))
                        {
                            //a string b number
                            return 1;
                        }
                        else
                        {
                            // a string b string
                            return a.CompareTo(b);
                        }
                    }
                }
            }
            return splitvalues.Length.CompareTo(otherMySort.splitvalues.Length);

        }
    }
票数 0
EN

Stack Overflow用户

发布于 2019-04-20 16:53:19

正如您从下面的测试代码中所看到的,代码工作正常。我做了一个小改动,添加了Sort()方法,使调用代码变得更容易。其他一切都是一样的。:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = { "1.7.1", "1.7.10", "1.7.2", "1.7.3", "1.7.4", "1.7.5", "1.7.6", "1.7.7", "1.7.8", "1.7.9" };
            string[] output = MySort.Sort(input);
        }
    }
    public class MySort : IComparable
    {
        private string[] splitvalues { get; set; }
        public string filename { get; set; }

        public MySort(string filename)
        {
            this.filename = filename;
            splitvalues = filename.Split(new char[] { '.', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

        }
        public static string[] Sort(string[] input)
        {
            return input.Select(x => new MySort(x)).OrderBy(x => x).Select(x => x.filename).ToArray();
        }
        public int CompareTo(object other)
        {
            MySort otherMySort = (MySort)other;
            int min = Math.Min(this.splitvalues.Length, otherMySort.splitvalues.Length);

            for (int i = 0; i < min; i++)
            {
                string a = this.splitvalues[i];
                string b = otherMySort.splitvalues[i];

                if (a != b)
                {

                    int numberA = 0;
                    int numberB = 0;

                    if (int.TryParse(a, out numberA))
                    {
                        if (int.TryParse(b, out numberB))
                        {
                            int z = numberA.CompareTo(numberB);
                            //a & b are numbers
                            return numberA.CompareTo(numberB);
                        }
                        else
                        {
                            //a number b string
                            return -1;
                        }

                    }
                    else
                    {
                        if (int.TryParse(b, out numberB))
                        {
                            //a string b number
                            return 1;
                        }
                        else
                        {
                            // a string b string
                            return a.CompareTo(b);
                        }
                    }
                }
            }
            return splitvalues.Length.CompareTo(otherMySort.splitvalues.Length);

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

https://stackoverflow.com/questions/55764366

复制
相关文章

相似问题

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