This topic has not yet been rated - Rate this topic

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


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";
        }
    }
} 


Silverlight

Supported in: 5, 4

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

Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.