LINQ and File Directories

Many file system operations are essentially queries and are therefore well-suited to the LINQ approach.


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:


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.

See Also


LINQ to Objects