Les på engelsk Rediger

Del via


SearchOption Enum

Definition

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

public enum SearchOption
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum SearchOption
Inheritance
SearchOption
Attributes

Fields

Name Value Description
TopDirectoryOnly 0

Includes only the current directory in a search operation.

AllDirectories 1

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.

Examples

The following example lists all the directories and files that begin with the letter "c", as in "c:\". In this example, TopDirectoryOnly 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);

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

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

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.

Applies to

Produkt Versjoner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0