Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to find perticular folder size with it's sub directories, and want to print it to the label, in the asp.net... can any one help me out??? Thnx in adv...
Posted

 
Share this answer
 
use these code
C#
protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(FindFolderSize(new DirectoryInfo(Server.MapPath("~")), UnitType.KB, 0).ToString() + " KB");
    Response.Write(FindFolderSize(new DirectoryInfo(Server.MapPath("~")), UnitType.MB, 2).ToString() + " MB");
    Response.Write(FindFolderSize(new DirectoryInfo(Server.MapPath("~")), UnitType.GB, 5).ToString() + " GB");
}


public enum UnitType { KB = 1, MB = 2, GB = 3 }
///
/// Find folder size
///
/// Target folder
/// Unit type [KB, MB, GB]
/// Number to digits to round up
///
C#
public double FindFolderSize(DirectoryInfo d, UnitType u, int r)
{
    double divider = Math.Pow(1024, (int)u);
    double size = 0;
    foreach (FileInfo f in d.GetFiles())
        size += Convert.ToDouble(f.Length) / divider;
    foreach (DirectoryInfo c in d.GetDirectories())
        size += this.FindFolderSize(c, u, r);
    return Math.Round(size, r);
}
 
Share this answer
 
v2
Comments
aaa2009 22-Oct-12 10:02am    
Hello "vinoth kumar rajendran",

I used your solution and it worked in a great way.

Thanks a lot for this nice solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900