请问是否能利用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
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);
}
}
}
}
我才寫到一半,寫輸你了哈哈
@RosonJ: 那你也发啊,肯定不完全一样,我把威胁给园豆的话删除了
@会长:
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: 那你百分之百没有园豆了
@会长:
那沒關係,久沒寫了就當練習
@RosonJ: 赞
能。而且easy