محاسبه ی حجم کلی یک دایرکتوری
کد:
using System.IO;
public long CalculateDirectorySize(DirectoryInfo directory, bool includeSubdirectories)
{
long totalSize = 0;
// Examine all contained files.
FileInfo[] files = directory.GetFiles();
foreach (FileInfo file in files)
{
totalSize += file.Length;
}
// Examine all contained directories.
if (includeSubdirectories)
{
DirectoryInfo[] dirs = directory.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
totalSize += CalculateDirectorySize(dir, true);
}
}
return totalSize;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(CalculateDirectorySize(new DirectoryInfo( @"C:\WINDOWS\System32"), true).ToString());
}