目录获取和处理;
1 string path = ".";//表明要在当前所在的目录 2 //先定义目录信息变量 3 DirectoryInfo dir = new DirectoryInfo(path); 4 foreach(FileInfo ld in dir.GetFiles()) 5 { 6 string name = ld.Name; 7 long size = ld.Length; 8 DateTime time = ld.CreationTime; 9 Console.WriteLine("{0,-12:N0}{1,-20:g}{2}",size,name,time); 10 } 11 //制定目录名称 12 string pa = "E:/Cprogram"; 13 DirectoryInfo din = new DirectoryInfo(pa); 14 Console.WriteLine(dirsize(din).ToString()); 15 Console.ReadKey();
1 public static double dirsize(DirectoryInfo d) 2 { 3 double size = 0; 4 //寻找当前目录的文件,并计算其大小 5 FileInfo[] fis = d.GetFiles(); 6 foreach(FileInfo fi in fis) 7 { 8 size += fi.Length; 9 } 10 //寻找当前目录下面的目录,并计算每个目录的大小 11 DirectoryInfo[] dis = d.GetDirectories(); 12 foreach(DirectoryInfo di in dis) 13 { 14 size += dirsize(di); 15 } 16 return size; 17 }