CSharp examples for File IO:Directory
Calculate the Size of a Directory
using System;// w w w.jav a 2 s. co m using System.IO; static class MainClass { static void Main(string[] args) { DirectoryInfo dir = new DirectoryInfo("c:/a"); Console.WriteLine("Total size: " + CalculateDirectorySize(dir, true).ToString() + " bytes."); } static long CalculateDirectorySize(DirectoryInfo directory, bool includeSubdirectories) { long totalSize = 0; foreach (FileInfo file in directory.EnumerateFiles()) { totalSize += file.Length; } if (includeSubdirectories) { foreach (DirectoryInfo dir in directory.EnumerateDirectories()) { totalSize += CalculateDirectorySize(dir, true); } } return totalSize; } }