SearchOption Enumeration

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Enumeration SearchOption
[ComVisibleAttribute(true)]
public enum SearchOption

Members

Member name Description
TopDirectoryOnly Includes only the current directory in a search.
AllDirectories Includes the current directory and all the subdirectories in a search operation. This option includes reparse points like mounted drives and symbolic links in the search.

Remarks

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.

Examples

The following code example lists all of the directories and files that begin with the letter "c" in the "My Documents" folder. In this example, the SearchOption is used to specify not to search all subdirectories.

Imports System.IO
Imports System.Collections.Generic

Class Example
    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

        Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        Dim searchPattern As String = "c*"

        Dim directories As IEnumerable(Of String) =
            Directory.EnumerateDirectories(path, searchPattern, SearchOption.TopDirectoryOnly)

        Dim files As IEnumerable(Of String) =
            Directory.EnumerateFiles(path, searchPattern, SearchOption.TopDirectoryOnly)

        outputBlock.Text &= String.Format(
            "Directories that begin with the letter 'c' in {0}", path) & vbCrLf

        For Each dir As String In directories
            outputBlock.Text &= dir & vbCrLf
        Next dir

        outputBlock.Text &= vbCrLf
        outputBlock.Text &= String.Format(
            "Files that begin with the letter 'c' in {0}", path) & vbCrLf

        For Each file As String In files
            outputBlock.Text &= file & vbCrLf
        Next file
    End Sub
End Class
using System;
using System.IO;
using System.Collections.Generic;

class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string searchPattern = "c*";

        IEnumerable<string> directories =
            Directory.EnumerateDirectories(path, searchPattern, SearchOption.TopDirectoryOnly);

        IEnumerable<string> files =
            Directory.EnumerateFiles(path, searchPattern, SearchOption.TopDirectoryOnly);

        outputBlock.Text += String.Format(
            "Directories that begin with the letter \"c\" in {0}", path) + "\n";
        foreach (string dir in directories)
        {
            outputBlock.Text += dir + "\n";
        }

        outputBlock.Text += "\n";
        outputBlock.Text += String.Format(
            "Files that begin with the letter \"c\" in {0}", path) + "\n";
        foreach (string file in files)
        {
            outputBlock.Text += file + "\n";
        }
    }
} 

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference