首页 新闻 赞助 找找看

请问是否能利用C#倒出 "按照档案数量多寡倒序文件夹" 数据

0
悬赏园豆:10 [已解决问题] 解决于 2020-08-07 16:24

请问是否能利用C#倒出 "按照档案数量多寡倒序文件夹" 数据
像是 :

C:
    A
        1.txt
        2.txt
        3.txt
    B
        F
            4.txt
            5.txt
            6.txt
            7.txt
    C
        9.txt
        10.txt
        11.txt
        12.txt
        13.txt
        G
            7.txt
            8.txt

得出 :

folder_name , folder_file_count
C           , 5
F           , 4
A           , 3
G           , 2
B           , 0
我问故我在的主页 我问故我在 | 初学一级 | 园豆:29
提问于:2020-08-07 15:04
< >
分享
最佳答案
1
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace OrderDir
{
    class Program
    {
        static void Main(string[] args)
        {
            var rootPath = @"F:\Joey\TalksLog";
            if (args != null && args.Any())
            {
                rootPath = args[0];
            }
            List<KeyValuePair<string, int>> dic = new List<KeyValuePair<string, int>>();
            BeginFuck(rootPath, dic);
            var result = dic.OrderByDescending(d => d.Value);
            Print(result);
        }

        private static void Print(IOrderedEnumerable<KeyValuePair<string, int>> result)
        {
            foreach (var item in result)
            {
                Console.WriteLine($"{item.Key}, {item.Value}");
            }
            Console.ReadLine();
        }

        private static void BeginFuck(string path, List<KeyValuePair<string, int>> dic)
        {
            dic.Add(new KeyValuePair<string, int>(path, Directory.GetFiles(path).Length));
            var children = Directory.GetDirectories(path);
            foreach (var child in children)
            {
                BeginFuck(child, dic);
            }
        }
    }
}

收获园豆:10
会长 | 专家六级 |园豆:12401 | 2020-08-07 15:46

我才寫到一半,寫輸你了哈哈

RosonJ | 园豆:4910 (老鸟四级) | 2020-08-07 15:57

@RosonJ: 那你也发啊,肯定不完全一样,我把威胁给园豆的话删除了

会长 | 园豆:12401 (专家六级) | 2020-08-07 16:09

@会长:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace FolderInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入路徑:");
            string path = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(path))
                return;

            DirectoryInfo di = new DirectoryInfo(path);
            List<Folder> list = Counter(di).OrderBy(d => d.Count).ToList();

            Console.WriteLine("folder_name , folder_file_count");
            list.ForEach(l =>
            {
                Console.WriteLine(l.Name + " , " + l.Count);
            });

            Console.ReadLine();
        }

        static List<Folder> Counter(DirectoryInfo di)
        {
            List<Folder> list = new List<Folder>();
            Folder folder = new Folder
            {
                Name = di.Name,
                Count = di.EnumerateFiles().Count()
            };
            list.Add(folder);

            foreach (var subDi in di.EnumerateDirectories())
            {
                list.AddRange(Counter(subDi));
            }

            return list;
        }

        public class Folder
        {
            public string Name { get; set; }

            public int Count { get; set; }
        }
    }
}

發你這就好,我覺得大同小異

RosonJ | 园豆:4910 (老鸟四级) | 2020-08-07 16:14

太厉害! 感谢两位大神

我问故我在 | 园豆:29 (初学一级) | 2020-08-07 16:25

@RosonJ: 那你百分之百没有园豆了

会长 | 园豆:12401 (专家六级) | 2020-08-07 16:26

@会长:
那沒關係,久沒寫了就當練習

RosonJ | 园豆:4910 (老鸟四级) | 2020-08-07 16:27

@RosonJ: 赞

会长 | 园豆:12401 (专家六级) | 2020-08-07 16:28
其他回答(1)
0

能。而且easy

花飘水流兮 | 园豆:13560 (专家六级) | 2020-08-07 15:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册