Saturday, May 30, 2009

Size of Folder in Bytes (Deep or Shallow)

using System;
using System.IO;

public class Folder {
private static double sizeInBytes;

public static double Size(string directory, bool deep) {
DirectoryInfo dir = new DirectoryInfo(directory);
foreach(FileInfo f in dir.GetFiles()) {
sizeInBytes += f.Length;
}
if(deep) {
foreach(DirectoryInfo d in dir.GetDirectories()) {
Size(d.FullName, deep);
}
}
return sizeInBytes;
}

static void Main() {
Console.WriteLine("The total folder size in bytes is {0}", Folder.Size(@"c:\sonysys", true));
}
}

1 comment:

  1. Dear Sir,

    I hope you are doing well. I got this email address from one of your contribution web site. I have launched a web site www.codegain.com and it is basically aimed C#,JAVA,VB.NET,ASP.NET,AJAX,Sql Server,Oracle,WPF,WCF and etc resources, programming help, articles, code snippet, video demonstrations and problems solving support. I would like to invite you as an author and a supporter.
    Looking forward to hearing from you and hope you will join with us soon.

    Thank you
    RRaveen
    Founder CodeGain.com

    ReplyDelete