SearchOption 枚举

定义

指定是搜索当前目录,还是搜索当前目录及其所有子目录。

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

字段

AllDirectories 1

在搜索操作中包括当前目录和所有它的子目录。 此选项在搜索中包括重解析点,比如安装的驱动器和符号链接。

TopDirectoryOnly 0

仅在搜索操作中包括当前目录。

示例

以下示例列出了以字母“c”开头的所有目录和文件,如“c:\”。 在此示例中, TopDirectoryOnly 用于指定仅应搜索顶级目录。

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.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 搜索中选择,并且目录结构包含创建循环的链接,则搜索操作将进入无限循环。

适用于