namespace FileSizeCalculator{ class Program { static async Task Main(string[] args) { Console.WriteLine("Enter directory paths separated by commas:"); string input = Console.ReadLine(); List<string> directoryPaths = new List<string>(input.Split(',')); long[] sizes = await GetTotalSizeAsync(directoryPaths); long totalSize = sizes.Sum(); Console.WriteLine($"Total number of files: {GetFileCount(directoryPaths)}"); Console.WriteLine($"Total size of files (bytes): {totalSize}"); }
Task.WhenAll
static async Task<long> GetTotalSizeAsync(List<string> directoryPaths) { long totalSize = 0; await Task.WhenAll(directoryPaths.Select(async directoryPath => { string[] files = await Task.Run(() => Directory.GetFiles(directoryPath)); foreach (string file in files) { totalSize += await GetFileSizeAsync(file); } })); return totalSize; } static int GetFileCount(List<string> directoryPaths) { int totalFileCount = 0; foreach (string directoryPath in directoryPaths) { string[] files = Directory.GetFiles(directoryPath); totalFileCount += files.Length; } return totalFileCount; } }}