usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacefilelist{ Public classProgram { Public Static voidMain (string[] args) {Console.WriteLine (); LongLength = Getdirectorylength (@"D:\Folder\ Dish four"); Console.WriteLine (length. ToString ("N0")); Console.WriteLine (); LongLength2 = FileSize (@"D:\Folder\ Dish four"); Console.WriteLine (length2. ToString ("N0")); Console.WriteLine (DateTime.Now.Millisecond); } Public Static LongGetdirectorylength (stringDirpath) { //determines whether a given path exists and exits if it does not exist if(!directory.exists (dirpath))return 0; LongLen =0; //define a DirectoryInfo objectDirectoryInfo di =NewDirectoryInfo (Dirpath); //get the size of all files in the Di directory through the GetFiles method foreach(FileInfo fiinchdi. GetFiles ()) {Len+=fi. Length; } //get all the folders in Di and coexist in a new object array for recursiondirectoryinfo[] dis =di. GetDirectories (); if(Dis. Length >0) { for(inti =0; i < Dis. Length; i++) {len+=getdirectorylength (Dis[i]. FullName); } } returnLen; } //It is also the idea of using recursion, only to judge by the exits method of the file class.//whether the corresponding file is in the given path Public Static LongFileSize (stringFilePath) { Longtemp =0; //determines whether the current path points to a file if(File.exists (filePath) = =false) { string[] str1 =directory.getfilesystementries (FilePath); foreach(stringS1inchstr1) {Temp+=FileSize (S1); } } Else { //define a FileInfo object to associate with the file that FilePath points to,//to get its sizeFileInfo FileInfo =NewFileInfo (FilePath); returnfileinfo.length; } returntemp; } }}
Get the source of folder size in C # implementation