C#实现指定路径,扫描该路径下所有文件,统计文件类型及其对应数量
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 44 45 46 47 48 49 50 51 52 53 54 55
| using System; using System.Collections.Generic; using System.IO;
namespace FileExtensionScan { class Program { Dictionary<String, int> pList = new Dictionary<String, int>(); int fileNum = 0;
public void GetFileNum(string srcPath) { try { string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath); foreach (string file in fileList) { if (Directory.Exists(file)) GetFileNum(file); else { fileNum++; string ext = Path.GetExtension(file).ToLower(); if (pList.ContainsKey(ext) == false) pList.Add(ext, 1); else pList[ext]++; } }
} catch (Exception e) { Console.WriteLine(e.ToString()); } }
static void Main(string[] args) { Program p = new Program(); p.GetFileNum(args[0]); Console.WriteLine("扫描文件数目" + p.fileNum);
foreach (var dic in p.pList) { Console.WriteLine("{0} {1}", dic.Key, dic.Value); } } } }
|