How to: Query for the Total Number of Bytes in a Set of Folders (LINQ) (C#)

This example shows how to retrieve the total number of bytes used by all the files in a specified folder and all its subfolders.


The Sum method adds the values of all the items selected in the select clause. You can easily modify this query to retrieve the biggest or smallest file in the specified directory tree by calling the Min or Max method instead of Sum.

class QuerySize  
    public static void Main()  
        string startFolder = @"c:\program files\Microsoft Visual Studio 9.0\VC#";  
        // Take a snapshot of the file system.  
        // This method assumes that the application has discovery permissions  
        // for all folders under the specified path.  
        IEnumerable<string> fileList = System.IO.Directory.GetFiles(startFolder, "*.*", System.IO.SearchOption.AllDirectories);  
        var fileQuery = from file in fileList  
                        select GetFileLength(file);  
        // Cache the results to avoid multiple trips to the file system.  
        long[] fileLengths = fileQuery.ToArray();  
        // Return the size of the largest file  
        long largestFile = fileLengths.Max();  
        // Return the total number of bytes in all the files under the specified folder.  
        long totalBytes = fileLengths.Sum();  
        Console.WriteLine("There are {0} bytes in {1} files under {2}",  
            totalBytes, fileList.Count(), startFolder);  
        Console.WriteLine("The largest files is {0} bytes.", largestFile);  
        // Keep the console window open in debug mode.  
        Console.WriteLine("Press any key to exit.");  
    // This method is used to swallow the possible exception  
    // that can be raised when accessing the System.IO.FileInfo.Length property.  
    static long GetFileLength(string filename)  
        long retval;  
            System.IO.FileInfo fi = new System.IO.FileInfo(filename);  
            retval = fi.Length;  
        catch (System.IO.FileNotFoundException)  
            // If a file is no longer present,  
            // just add zero bytes to the total.  
            retval = 0;  
        return retval;  

If you only have to count the number of bytes in a specified directory tree, you can do this more efficiently without creating a LINQ query, which incurs the overhead of creating the list collection as a data source. The usefulness of the LINQ approach increases as the query becomes more complex, or when you have to run multiple queries against the same data source.

The query calls out to a separate method to obtain the file length. It does this in order to consume the possible exception that will be raised if the file was deleted on another thread after the FileInfo object was created in the call to GetFiles. Even though the FileInfo object has already been created, the exception can occur because a FileInfo object will try to refresh its Length property with the most current length the first time the property is accessed. By putting this operation in a try-catch block outside the query, the code follows the rule of avoiding operations in queries that can cause side-effects. In general, great care must be taken when you consume exceptions to make sure that an application is not left in an unknown state.

Compiling the Code

Create a C# console application project, with using directives for the System.Linq and System.IO namespaces.

See also