SearchOption Enumeration

SearchOption Enumeration


Specifies whether to search the current directory, or the current directory and all subdirectories.

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

public enum SearchOption

Member nameDescription

Includes the current directory and all its subdirectories in a search operation. This option includes reparse points such as mounted drives and symbolic links in the search.


Includes only the current directory in a search operation.

If you choose AllDirectories in your search and the directory structure contains a link that creates a loop, the search operation enters an infinite loop.

The following example lists all the directories and files that begin with the letter "c", as in "c:\". In this example, the SearchOption is used to specify that only the top-level directory should be searched.

using System;
using System.IO;

class App
    public static void Main()
        // Specify the directory you want to manipulate.
        string path = @"c:\";
        string searchPattern = "c*";

        DirectoryInfo di = new DirectoryInfo(path);
        DirectoryInfo[] directories = 
            di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly);

        FileInfo[] files = 
            di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);

            "Directories that begin with the letter \"c\" in {0}", path);
        foreach (DirectoryInfo dir in directories)
                "{0,-25} {1,25}", dir.FullName, dir.LastWriteTime);

            "Files that begin with the letter \"c\" in {0}", path);
        foreach (FileInfo file in files)
                "{0,-25} {1,25}", file.Name, file.LastWriteTime);
    } // Main()
} // App()

Universal Windows Platform
Available since 10
.NET Framework
Available since 2.0
Available since 4.0
Return to top
© 2015 Microsoft