You can make use of this function:
static long CalculateDirectorySize(DirectoryInfo directory, bool includeSubdirectories)
{
long totalSize = 0;
// Examine all contained files.
foreach (FileInfo file in directory.EnumerateFiles())
{
totalSize += file.Length;
}
// Examine all contained directories.
if (includeSubdirectories)
{
foreach (DirectoryInfo dir in directory.EnumerateDirectories())
{
totalSize += CalculateDirectorySize(dir, true);
}
}
return totalSize;
}
{
long totalSize = 0;
// Examine all contained files.
foreach (FileInfo file in directory.EnumerateFiles())
{
totalSize += file.Length;
}
// Examine all contained directories.
if (includeSubdirectories)
{
foreach (DirectoryInfo dir in directory.EnumerateDirectories())
{
totalSize += CalculateDirectorySize(dir, true);
}
}
return totalSize;
}
How to call:
DirectoryInfo dir = new DirectoryInfo("c:\\windows");
long size = CalculateDirectorySize(dir, true);
MessageBox.Show(size.ToString() + " Bytes");
long size = CalculateDirectorySize(dir, true);
MessageBox.Show(size.ToString() + " Bytes");
No comments:
Post a Comment