共用方式為


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 搜尋中選擇 ,且目錄結構包含建立循環的連結,則搜尋作業會進入無限迴圈。

適用於