Wednesday 13 December 2017

C# Calculate Directory (Folder) & Sub Directory (Sub Folder) File Size

 public static long DirSize(DirectoryInfo d)
    {
        long size = 0;
        // Add file sizes.
        FileInfo[] fis = d.GetFiles();
        foreach (FileInfo fi in fis)
        {
            size += fi.Length;
        }
        // Add subdirectory sizes.
        DirectoryInfo[] dis = d.GetDirectories();
        foreach (DirectoryInfo di in dis)
        {
            size += DirSize(di);
        }
        return size;
    }



 String fldName1 = "~/" +   hfFileLocation.Value;
            string path = Server.MapPath(fldName1);
double fsize = 0.00;
fsize = DirSize(new DirectoryInfo(path));
Response.Write(fsize );
double fsize_MB = 0.00;
 fsize_MB = fsize / (1024 * 1024);
Response.Write("File Size in MB : "+fsize_MB );


No comments:

Post a Comment

Upload valid file in C#

    protected bool CheckFileExtandLength(HttpPostedFile HtmlDocFile)     {         try         {             Dictionary<string, byte[]>...