private long GetDirectorySizeMethod1(string directory)
{
long directorySize = 0;
DirectoryInfo di = new DirectoryInfo(directory);
if (!di.Exists)
{
Console.WriteLine("Directory {0} is not exist!", directory);
return 0;
}
foreach (FileInfo fi in di.GetFiles())
{
directorySize += fi.Length;
}
DirectoryInfo[] dirs = di.GetDirectories();
foreach (DirectoryInfo sondir in dirs)
{
directorySize += GetDirectorySizeMethod1(sondir.FullName);
}
return directorySize;
}
private long GetDirectorySizeMethod2(string directory)
{
long directorySize = 0;
if (File.Exists(directory))
{
FileInfo fi = new FileInfo(directory);
return fi.Length;
}
else
{
string[] strs = Directory.GetFileSystemEntries(directory);
foreach (string str in strs)
{
directorySize += GetDirectorySizeMethod2(str);
}
}
return directorySize;
}