共用方式為


DirectoryInfo.GetDirectories 方法

定義

傳回目前目錄的子目錄。

多載

GetDirectories()

傳回目前目錄的子目錄。

GetDirectories(String)

傳回在目前 DirectoryInfo 中,符合指定的搜尋準則的目錄陣列。

GetDirectories(String, EnumerationOptions)

傳回目前 DirectoryInfo 中符合所指定搜尋模式和列舉選項的目錄陣列。

GetDirectories(String, SearchOption)

傳回目前 DirectoryInfo 中的目錄陣列,這些目錄符合指定的搜尋準則並使用值來判斷是否搜尋子目錄。

GetDirectories()

來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs

傳回目前目錄的子目錄。

public:
 cli::array <System::IO::DirectoryInfo ^> ^ GetDirectories();
public System.IO.DirectoryInfo[] GetDirectories ();
member this.GetDirectories : unit -> System.IO.DirectoryInfo[]
Public Function GetDirectories () As DirectoryInfo()

傳回

DirectoryInfo 物件的陣列。

例外狀況

DirectoryInfo 物件中封裝的路徑無效,例如位於未對應的磁碟機上。

呼叫端沒有必要的權限。

呼叫端沒有必要的權限。

範例

下列範例會擷取根目錄中的所有目錄,並顯示目錄名稱。

using namespace System;
using namespace System::IO;
int main()
{
   
   // Make a reference to a directory.
   DirectoryInfo^ di = gcnew DirectoryInfo( "c:\\" );
   
   // Get a reference to each directory in that directory.
   array<DirectoryInfo^>^diArr = di->GetDirectories();
   
   // Display the names of the directories.
   Collections::IEnumerator^ myEnum = diArr->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      DirectoryInfo^ dri = safe_cast<DirectoryInfo^>(myEnum->Current);
      Console::WriteLine( dri->Name );
   }
}
using System;
using System.IO;

public class GetDirectoriesTest
{
    public static void Main()
    {

        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("c:\\");

        // Get a reference to each directory in that directory.
        DirectoryInfo[] diArr = di.GetDirectories();

        // Display the names of the directories.
        foreach (DirectoryInfo dri in diArr)
            Console.WriteLine(dri.Name);
    }
}
open System.IO

// Make a reference to a directory.
let di = DirectoryInfo "c:\\"

// Get a reference to each directory in that directory.
let diArr = di.GetDirectories()

// Display the names of the directories.
for dri in diArr do
    printfn $"{dri.Name}"
Imports System.IO

Public Class GetDirectoriesTest

    Public Shared Sub Main()
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("c:\")
        ' Get a reference to each directory in that directory.
        Dim diArr As DirectoryInfo() = di.GetDirectories()
        ' Display the names of the directories.
        Dim dri As DirectoryInfo
        For Each dri In diArr
            Console.WriteLine(dri.Name)
        Next dri
    End Sub
End Class

備註

如果沒有子目錄,這個方法會傳回空陣列。 這個方法不是遞歸的。

這個方法會預先填入下列 DirectoryInfo 屬性的值:

另請參閱

適用於

GetDirectories(String)

來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs

傳回在目前 DirectoryInfo 中,符合指定的搜尋準則的目錄陣列。

public:
 cli::array <System::IO::DirectoryInfo ^> ^ GetDirectories(System::String ^ searchPattern);
public System.IO.DirectoryInfo[] GetDirectories (string searchPattern);
member this.GetDirectories : string -> System.IO.DirectoryInfo[]
Public Function GetDirectories (searchPattern As String) As DirectoryInfo()

參數

searchPattern
String

要比對目錄名稱的搜尋字串。 這個參數可以包含有效常值路徑與萬用 (* 和 ?) 字元的組合,但是不支援規則運算式。

傳回

DirectoryInfo 類型的陣列,符合 searchPattern

例外狀況

.NET Framework 和 2.1 之前的 .NET Core 版本:searchPattern包含 方法所GetInvalidPathChars()定義的一或多個無效字元。

searchPatternnull

DirectoryInfo 物件中封裝的路徑無效 (例如它位於未對應的磁碟機上)。

呼叫端沒有必要的權限。

範例

下列範例會計算包含指定字母之路徑中的目錄。

using namespace System;
using namespace System::IO;
int main()
{
   try
   {
      DirectoryInfo^ di = gcnew DirectoryInfo( "c:\\" );
      
      // Get only subdirectories that contain the letter "p."
      array<DirectoryInfo^>^dirs = di->GetDirectories( "*p*" );
      Console::WriteLine( "The number of directories containing the letter p is {0}.", dirs->Length );
      Collections::IEnumerator^ myEnum = dirs->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         DirectoryInfo^ diNext = safe_cast<DirectoryInfo^>(myEnum->Current);
         Console::WriteLine( "The number of files in {0} is {1}", diNext, diNext->GetFiles()->Length );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }

}
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            DirectoryInfo di = new DirectoryInfo(@"c:\");

            // Get only subdirectories that contain the letter "p."
            DirectoryInfo[] dirs = di.GetDirectories("*p*");
            Console.WriteLine("The number of directories containing the letter p is {0}.", dirs.Length);

            foreach (DirectoryInfo diNext in dirs)
            {
                Console.WriteLine("The number of files in {0} is {1}", diNext,
                    diNext.GetFiles().Length);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
open System.IO

try
    let di = DirectoryInfo @"c:\"

    // Get only subdirectories that contain the letter "p."
    let dirs = di.GetDirectories "*p*"
    printfn $"The number of directories containing the letter p is {dirs.Length}."

    for diNext in dirs do
        printfn $"The number of files in {diNext} is {diNext.GetFiles().Length}"
with e ->
    printfn $"The process failed: {e}"
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim di As DirectoryInfo = New DirectoryInfo("c:\")

        Try
            'Get only subdirectories that contain the letter "p."
            Dim dirs As DirectoryInfo() = di.GetDirectories("*p*")
            Console.WriteLine("The number of directories containing the letter p is {0}.", dirs.Length)
            Dim diNext As DirectoryInfo
            For Each diNext In dirs
                Console.WriteLine("The number of files in {0} is {1}", diNext, _
                 diNext.GetFiles().Length)
            Next

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

備註

searchPattern 可以是常值和通配符的組合,但不支援正則表達式。 在中 searchPattern允許下列通配符規範。

通配符規範 相符項
* (星號) 該位置中的零或多個字元。
? (問號) 該位置中的零或一個字元。

通配符以外的字元是常值字元。 例如,字串 「*t」 會搜尋以字母 「t」 結尾的所有名稱。 ". 字串 “s*” 會 searchPattern 以字母 “s” 開頭搜尋所有名稱 path

這個方法會預先填入下列 DirectoryInfo 屬性的值:

另請參閱

適用於

GetDirectories(String, EnumerationOptions)

來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs

傳回目前 DirectoryInfo 中符合所指定搜尋模式和列舉選項的目錄陣列。

public:
 cli::array <System::IO::DirectoryInfo ^> ^ GetDirectories(System::String ^ searchPattern, System::IO::EnumerationOptions ^ enumerationOptions);
public System.IO.DirectoryInfo[] GetDirectories (string searchPattern, System.IO.EnumerationOptions enumerationOptions);
member this.GetDirectories : string * System.IO.EnumerationOptions -> System.IO.DirectoryInfo[]
Public Function GetDirectories (searchPattern As String, enumerationOptions As EnumerationOptions) As DirectoryInfo()

參數

searchPattern
String

要比對目錄名稱的搜尋字串。 這個參數可以包含有效常值路徑與萬用 (* 和 ?) 字元的組合,但是不支援規則運算式。

enumerationOptions
EnumerationOptions

物件,描述要使用的搜尋和列舉組態。

傳回

符合 searchPatternenumerationOptionsDirectoryInfo 型別的陣列。

例外狀況

.NET Framework 和 2.1 之前的 .NET Core 版本:searchPattern包含 方法所GetInvalidPathChars()定義的一或多個無效字元。

searchPatternnull

DirectoryInfo 物件中封裝的路徑無效 (例如它位於未對應的磁碟機上)。

呼叫端沒有必要的權限。

備註

searchPattern 可以是常值和通配符的組合,但不支援正則表達式。 在中 searchPattern允許下列通配符規範。

通配符規範 相符項
* (星號) 該位置中的零或多個字元。
? (問號) 該位置中的零或一個字元。

通配符以外的字元是常值字元。 例如,字串 「*t」 會搜尋以字母 「t」 結尾的所有名稱。 ". 字串 “s*” 會 searchPattern 以字母 “s” 開頭搜尋所有名稱 path

這個方法會預先填入下列 DirectoryInfo 屬性的值:

適用於

GetDirectories(String, SearchOption)

來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs

傳回目前 DirectoryInfo 中的目錄陣列,這些目錄符合指定的搜尋準則並使用值來判斷是否搜尋子目錄。

public:
 cli::array <System::IO::DirectoryInfo ^> ^ GetDirectories(System::String ^ searchPattern, System::IO::SearchOption searchOption);
public System.IO.DirectoryInfo[] GetDirectories (string searchPattern, System.IO.SearchOption searchOption);
member this.GetDirectories : string * System.IO.SearchOption -> System.IO.DirectoryInfo[]
Public Function GetDirectories (searchPattern As String, searchOption As SearchOption) As DirectoryInfo()

參數

searchPattern
String

要比對目錄名稱的搜尋字串。 這個參數可以包含有效常值路徑與萬用 (* 和 ?) 字元的組合,但是不支援規則運算式。

searchOption
SearchOption

其中一個列舉值,指定搜尋作業應該只包含目前目錄還是包含所有子目錄。

傳回

DirectoryInfo 類型的陣列,符合 searchPattern

例外狀況

.NET Framework 和 2.1 之前的 .NET Core 版本:searchPattern包含 方法所GetInvalidPathChars()定義的一或多個無效字元。

searchPatternnull

searchOption 不是有效的 SearchOption 值。

DirectoryInfo 物件中封裝的路徑無效 (例如它位於未對應的磁碟機上)。

呼叫端沒有必要的權限。

範例

下列範例會列出以 「c:\」 字母 「c」 開頭的所有目錄和檔案。

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()
open System.IO

// Specify the directory you want to manipulate.
let path = @"c:\"
let searchPattern = "c*"

let di = DirectoryInfo path
let directories = di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly)

let files = di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly)

printfn $"Directories that begin with the letter \"c\" in {path}"
for dir in directories do
    printfn $"{dir.FullName,-25} {dir.LastWriteTime,25}"

printfn $"\nFiles that begin with the letter \"c\" in {path}"
for file in files do
    printfn $"{file.Name,-25} {file.LastWriteTime,25}"
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

備註

searchPattern 可以是常值和通配符的組合,但不支援正則表達式。 在中 searchPattern允許下列通配符規範。

通配符規範 相符項
* (星號) 該位置中的零或多個字元。
? (問號) 該位置中的零或一個字元。

通配符以外的字元是常值字元。 例如,字串 「*t」 會搜尋以字母 「t」 結尾的所有名稱。 ". 字串 “s*” 會 searchPattern 以字母 “s” 開頭搜尋所有名稱 path

如果沒有子目錄,或沒有子目錄符合 searchPattern 參數,這個方法會傳回空陣列。

這個方法會預先填入下列 DirectoryInfo 屬性的值:

另請參閱

適用於