SearchOption Enumeration

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

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

<ComVisibleAttribute(True)> _
Public Enumeration SearchOption

Member nameDescription
TopDirectoryOnlyIncludes only the current directory in a search.
AllDirectoriesIncludes 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.

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 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


Supported in: 5, 4

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

Community Additions