Get the source of folder size in C # implementation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.