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

Getting Available Server Disk Space (Total Size / Free Space) SQL Query

SELECT distinct(volume_mount_point), total_bytes/1048576 as Size_in_MB, total_bytes/1048576/1024 as Size_in_GB, available_bytes/1048576 ...