0

I'm writing a console application and i want it to display the total size of all the files in a certain directory an example of the output would be this

Files in: C:\Windows
Total files: 49
Total size of all files: 7121424 bytes

Here is what i currently have:

            if (menuOption == "3")
        {
            Console.Clear();
            Console.WriteLine("Files in C:\\windows");
            Console.WriteLine("");
            DirectoryInfo folderInfo = new DirectoryInfo("C:\\Windows");
            FileInfo[] files = folderInfo.GetFiles();

            for (numFiles = 0; numFiles < files.Length; numFiles++)
            { 
            }
            Console.Write("Total Files: {0}",numFiles);
        }

As you can see i have already made it so that it gets the total amount of files in C:\Windows but im unsure how to make it add all the file sizes together. Hope you guys can give me some insight, thanks.

Ben
  • 45
  • 7

2 Answers2

6

Use Length property of FileInfo

var totalSize = files.Sum(x => x.Length);
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

By using the FileInfo.Length Property

long totalFileSize = 0;
for (numFiles = 0; numFiles < files.Length; numFiles++)
{
    totalFileSize += files[numFiles].Length;
}
t3chb0t
  • 16,340
  • 13
  • 78
  • 118