LINQ and File Directories
Many file system operations are essentially queries and are therefore well-suited to the LINQ approach.
Note
If you want to perform programmatic queries against the contents of multiple types of files and documents, consider using the Windows Desktop Search Engine. Although it cannot currently be queried with LINQ, it does provide a powerful indexing service that effectively manages the complexities of the file system.
Note that the queries in this section are non-destructive. They are not used to change the contents of the original files or folders. This follows the rule that queries should not cause any side-effects. In general, any code (including queries that perform create / update / delete operators) that modifies source data should be kept separate from the code that just queries the data.
This section contains the following topics:
How to: Query for Files with a Specified Attribute or Name
Shows how to search for files by examining one or more properties of its FileInfo object.How to: Group Files by Extension (LINQ)
Shows how to return groups of FileInfo object based on their file name extension.How to: Query for the Total Number of Bytes in a Set of Folders (LINQ)
Shows how to return the total number of bytes in all the files in a specified directory tree.How to: Compare the Contents of Two Folders (LINQ)s
Shows how to return all the files that are present in two specified folders, and also all the files that are present in one folder but not the other.How to: Query for the Largest File or Files in a Directory Tree (LINQ)
Shows how to return the largest or smallest file, or a specified number of files, in a directory tree.How to: Query for Duplicate Files in a Directory Tree (LINQ)
Shows how to group for all file names that occur in more than one location in a specified directory tree. Also shows how to perform more complex comparisons based on a custom comparer.How to: Query the Contents of Files in a Folder (LINQ)
Shows how to iterate through folders in a tree, open each file, and query the file's contents.
Comments
There is some complexity involved in creating a data source that accurately represents the contents of the file system and handles exceptions gracefully. The examples in this section create a snapshot collection of FileInfo objects that represents all the files under a specified root folder and all its subfolders. The actual state of each FileInfo may change in the time between when you begin and end executing a query. For example, you can create a list of FileInfo objects to use as a data source. If you try to access the Length property in a query, the FileInfo object will try to access the file system to update the value of Length. If the file no longer exists, you will get a FileNotFoundException in your query, even though you are not querying the file system directly. Some queries in this section use a separate method that consumes these particular exceptions in certain cases. Another option is to keep your data source updated dynamically by using the FileSystemWatcher.