Export (0) Print
Expand All

DirectoryInfo.EnumerateFiles Method (String, SearchOption)

Returns an enumerable collection of file information that matches a specified search pattern and search subdirectory option.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public IEnumerable<FileInfo> EnumerateFiles(
	string searchPattern,
	SearchOption searchOption
)

Parameters

searchPattern
Type: System.String
The search string. The default pattern is "*", which returns all files.
searchOption
Type: System.IO.SearchOption
One of the enumeration values that specifies whether the search operation should include only the current directory or all subdirectories. The default value is TopDirectoryOnly.

Return Value

Type: System.Collections.Generic.IEnumerable<FileInfo>
An enumerable collection of files that matches searchPattern and searchOption.

ExceptionCondition
ArgumentNullException

searchPattern is null.

ArgumentOutOfRangeException

searchOption is not a valid SearchOption value.

DirectoryNotFoundException

The path encapsulated in the FileInfo object is invalid (for example, it is on an unmapped drive).

SecurityException

The caller does not have the required permission.

The following wildcard specifiers are permitted in the searchPattern parameter.

Wildcard character

Description

*

Zero or more characters.

?

Exactly one character.

The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned; when you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

This method pre-populates the values of the following FileInfo properties:

The returned collection is not cached; each call to the GetEnumerator method on the collection will start a new enumeration.

The following example, starting from a specified directory, uses this method and the EnumerateDirectories method to enumerate directories and files for files that have a .txt extension. It then uses the ReadLines method to read each line of the file and display it if it contains the word "Microsoft".

If you have authorized access to all the files in your search, you can obtain the same results with a LINQ query. For an example, see the Directory.EnumerateFiles(String, String, SearchOption) method.


using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        // Create a DirectoryInfo object of the starting directory.
        DirectoryInfo diTop = new DirectoryInfo(@"d:\");
        try
        {
            // Enumerate the files just in the top directory.
            foreach (var fi in diTop.EnumerateFiles())
            {
                try
                {
                    // Display each file over 10 MB;
                    if (fi.Length > 10000000)
                    {
                        Console.WriteLine("{0}\t\t{1}", fi.FullName, 
                                        fi.Length.ToString("N0"));
                    }
                }
                // Catch unauthorized access to a file.
                catch (UnauthorizedAccessException UnAuthTop)
                {
                    Console.WriteLine("{0}", UnAuthTop.Message);
                }
            }
            // Enumerate all subdirectories.
            foreach (var di in diTop.EnumerateDirectories("*"))
            {
                try
                {
                    // Enumerate each file in each subdirectory.
                    foreach (var fi in di.EnumerateFiles("*",
                    				SearchOption.AllDirectories))
                    {
                        try
                        {
                            // Display each file over 10 MB;
                            if (fi.Length > 10000000)
                            {
                                Console.WriteLine("{0}\t\t{1}", 
                                  fi.FullName, fi.Length.ToString("N0"));
                            }
                        }
                         // Catch unauthorized access to a file.
                        catch (UnauthorizedAccessException UnAuthFile)
                        {
                            Console.WriteLine("UnAuthFile: {0}", 
                                            UnAuthFile.Message);
                        }
                    }
                }
                // Catch unauthorized access to a subdirectory.
                catch (UnauthorizedAccessException UnAuthSubDir)
                {
                    Console.WriteLine("UnAuthSubDir: {0}", 
                                            UnAuthSubDir.Message);
                }
            }
        }
        // Catch error in directory path.
        catch (DirectoryNotFoundException DirNotFound)
        {
            Console.WriteLine("{0}", DirNotFound.Message);
        }
        // Catch unauthorized access to a first tier directory. 
        catch (UnauthorizedAccessException UnAuthDir)
        {
            Console.WriteLine("UnAuthDir: {0}", UnAuthDir.Message);
        }
        // Catch paths that are too long. 
	    catch (PathTooLongException LongPath)
	    {
	        Console.WriteLine("{0}", LongPath.Message);
	    }

    }
}


.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft