复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44using System; using System.Diagnostics; using System.IO; namespace ConsoleApp6 { class Program { static void Main(string[] args) { string dir = @"";//文件夹路径 // 消耗时间 long timeA = DateTime.Now.Ticks; int count = GetDirFileCount(dir); long timespan = DateTime.Now.Ticks - timeA; Console.WriteLine($"timespan is {timespan} ticks, filecounts is {count}"); // 消耗内存 Process process = Process.GetCurrentProcess(); long usedMemory = process.WorkingSet64; Console.WriteLine($"当前进程所耗的内存: {usedMemory / 1024} KB"); } // 获取文件夹数目 public static int GetDirFileCount(string dir) { if (string.IsNullOrEmpty(dir)) return 0; int filescount = 0; try { DirectoryInfo dirInfo = new DirectoryInfo(dir); filescount = dirInfo.GetFiles().Length; DirectoryInfo[] dirs = dirInfo.GetDirectories(); foreach (var item in dirs) { filescount += GetDirFileCount(item.FullName); } } catch (Exception ex) { } return filescount; } } }
最后
以上就是有魅力绿草最近收集整理的关于C# 递归获取文件夹文件数目并衡量代码性能的全部内容,更多相关C#内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复