SearchOption SearchOption SearchOption SearchOption Enum

定义

指定是搜索当前目录,还是搜索当前目录及其所有子目录。Specifies whether to search the current directory, or the current directory and all subdirectories.

public enum class SearchOption
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum SearchOption
type SearchOption = 
Public Enum SearchOption
继承
属性

字段

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

TopDirectoryOnly TopDirectoryOnly TopDirectoryOnly TopDirectoryOnly 0

仅在搜索操作中包括当前目录。Includes only the current directory in a search operation.

示例

以下示例列出所有目录和文件中以字母"c"开头的"c:\"。The following example lists all the directories and files that begin with the letter "c", as in "c:\". 在此示例中,TopDirectoryOnly用于指定应搜索仅顶级目录。In this example, TopDirectoryOnly is used to specify that only the top-level directory should be searched.

using namespace System;
using namespace System::IO;

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

        DirectoryInfo^ di = gcnew DirectoryInfo(path);
        array<DirectoryInfo^>^ directories =
            di->GetDirectories(searchPattern, SearchOption::TopDirectoryOnly);

        array<FileInfo^>^ files =
            di->GetFiles(searchPattern, SearchOption::TopDirectoryOnly);

        Console::WriteLine(
            "Directories that begin with the letter \"c\" in {0}", path);
        for each (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);
        for each (FileInfo^ file in files)
        {
            Console::WriteLine(
                "{0,-25} {1,25}", file->Name, file->LastWriteTime);
        }
    } // Main()
}; // App()

int main()
{
    App::Main();
}
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()
Imports System
Imports System.IO

Class App
    Public Shared Sub Main()
        ' Specify the directory you want to manipulate.
        Dim path As String = "c:\\"
        Dim searchPattern As String = "c*"

        Dim di As DirectoryInfo = New DirectoryInfo(path)
        Dim directories() As DirectoryInfo = _
            di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly)

        Dim files() As FileInfo = _
            di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly)

        Console.WriteLine( _
            "Directories that begin with the letter 'c' in {0}", path)
        Dim dir As DirectoryInfo
        For Each dir In directories
            Console.WriteLine( _
                "{0,-25} {1,25}", dir.FullName, dir.LastWriteTime)
        Next dir

        Console.WriteLine()
        Console.WriteLine( _
            "Files that begin with the letter 'c' in {0}", path)
        Dim file As FileInfo
        For Each file In files
            Console.WriteLine( _
                "{0,-25} {1,25}", file.Name, file.LastWriteTime)
        Next file
    End Sub
End Class

注解

如果在搜索中选择 AllDirectories 和目录结构中包含一个链接,创建一个循环,搜索操作将进入一个无限循环。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.

适用于