Directory.GetDirectories 方法

定義

傳回符合指定準則之子目錄的名稱。Returns the names of subdirectories that meet specified criteria.

多載

GetDirectories(String, String, SearchOption)

傳回指定目錄中符合指定搜尋模式之子目錄 (包含其路徑) 的名稱,並且選擇性地搜尋子目錄。Returns the names of the subdirectories (including their paths) that match the specified search pattern in the specified directory, and optionally searches subdirectories.

GetDirectories(String, String, EnumerationOptions)

傳回所指定目錄中符合所指定搜尋模式與列舉選項的子目錄名稱 (包含其路徑)。Returns the names of subdirectories (including their paths) that match the specified search pattern and enumeration options in the specified directory.

GetDirectories(String)

傳回指定之目錄中的子目錄名稱 (包括其路徑)。Returns the names of subdirectories (including their paths) in the specified directory.

GetDirectories(String, String)

傳回指定目錄中符合指定搜尋模式的子目錄名稱 (包括檔案的路徑)。Returns the names of subdirectories (including their paths) that match the specified search pattern in the specified directory.

GetDirectories(String, String, SearchOption)

傳回指定目錄中符合指定搜尋模式之子目錄 (包含其路徑) 的名稱,並且選擇性地搜尋子目錄。Returns the names of the subdirectories (including their paths) that match the specified search pattern in the specified directory, and optionally searches subdirectories.

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

參數

path
String

要搜尋之目錄的相對或絕對路徑。The relative or absolute path to the directory to search. 這個字串不會區分大小寫。This string is not case-sensitive.

searchPattern
String

要比對 path 中子目錄名稱的搜尋字串。The search string to match against the names of subdirectories in path. 這個參數可以包含有效常值與萬用字元的組合,但是不支援規則運算式。This parameter can contain a combination of valid literal and wildcard characters, but it doesn't support regular expressions.

searchOption
SearchOption

其中一個列舉值,這個值會指定搜尋作業應該包含所有子目錄或只包含目前目錄。One of the enumeration values that specifies whether the search operation should include all subdirectories or only the current directory.

傳回

String[]

符合指定之準則的子目錄完整名稱 (包括路徑) 陣列,如果找不到任何目錄則為空陣列。An array of the full names (including paths) of the subdirectories that match the specified criteria, or an empty array if no directories are found.

例外狀況

path 為零長度字串、只包含空格,或包含一或多個無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters. 您可以使用 GetInvalidPathChars() 方法查詢無效字元。You can query for invalid characters by using the GetInvalidPathChars() method.

-或--or- searchPattern 不包含有效模式。searchPattern does not contain a valid pattern.

pathsearchPatternnullpath or searchPattern is null.

searchOption 不是有效的 SearchOption 值。searchOption is not a valid SearchOption value.

呼叫端沒有必要的權限。The caller does not have the required permission.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

path 為檔案名稱。path is a file name.

指定的路徑無效 (例如,它位於未對應的磁碟機上)。The specified path is invalid (for example, it is on an unmapped drive).

範例

下列範例會計算路徑中以指定字母開頭的目錄數目。The following example counts the number of directories that begin with the specified letter in a path. 只會搜尋最上層目錄。Only the top-level directory is searched.

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            string[] dirs = Directory.GetDirectories(@"c:\", "p*", SearchOption.TopDirectoryOnly);
            Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length);
            foreach (string dir in dirs)
            {
                Console.WriteLine(dir);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Try
            Dim dirs As String() = Directory.GetDirectories("c:\", "p*", SearchOption.TopDirectoryOnly)
            Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length)
            Dim dir As String
            For Each dir In dirs
                Console.WriteLine(dir)
            Next
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

備註

path參數可以指定相對或絕對路徑資訊,但不區分大小寫。The path parameter can specify relative or absolute path information, and is not case-sensitive. 相對路徑資訊會解讀為相對於目前工作目錄。Relative path information is interpreted as relative to the current working directory. 若要取得目前的工作目錄,請參閱 GetCurrentDirectoryTo obtain the current working directory, see GetCurrentDirectory.

searchPattern 可以是常值和萬用字元的組合,但是不支援正則運算式。searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. 中允許下列萬用字元規範 searchPatternThe following wildcard specifiers are permitted in searchPattern.

萬用字元規範Wildcard specifier 相符項Matches
* (星號) * (asterisk) 該位置中的零或多個字元。Zero or more characters in that position.
?? (問號)(question mark) 該位置中的零或一個字元。Zero or one character in that position.

萬用字元以外的字元是常值字元。Characters other than the wildcard are literal characters. 例如, searchPattern 字串 " * t" 會搜尋 path 結尾為字母 "t" 的所有名稱。For example, the searchPattern string "*t" searches for all names in path ending with the letter "t". searchPattern字串 "s * " 會搜尋開頭為 path 字母 "s" 的所有名稱。The searchPattern string "s*" searches for all names in path beginning with the letter "s".

searchPattern 無法在兩個期間結束 ( "...") 或包含兩個句點 ( "...") 後面接著 DirectorySeparatorCharAltDirectorySeparatorChar ,也不可以包含任何不正確字元。searchPattern cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any invalid characters. 您可以使用 GetInvalidPathChars 方法查詢無效字元。You can query for invalid characters by using the GetInvalidPathChars method.

EnumerateDirectoriesGetDirectories 方法的差異如下:當您使用時 EnumerateDirectories ,您可以在傳回整個集合之前,開始列舉名稱集合; 當您使用時 GetDirectories ,您必須等候完整的名稱陣列傳回,然後才能存取陣列。The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array. 因此,當您使用許多檔案和目錄時, EnumerateDirectories 可能會更有效率。Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

如需一般 i/o 工作的清單,請參閱 一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

GetDirectories(String, String, EnumerationOptions)

傳回所指定目錄中符合所指定搜尋模式與列舉選項的子目錄名稱 (包含其路徑)。Returns the names of subdirectories (including their paths) that match the specified search pattern and enumeration options in the specified directory.

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

參數

path
String

要搜尋之目錄的相對或絕對路徑。The relative or absolute path to the directory to search. 這個字串不會區分大小寫。This string is not case-sensitive.

searchPattern
String

要比對 path 中子目錄名稱的搜尋字串。The search string to match against the names of subdirectories in path. 這個參數可以包含有效常值與萬用字元的組合,但是不支援規則運算式。This parameter can contain a combination of valid literal and wildcard characters, but it doesn't support regular expressions.

enumerationOptions
EnumerationOptions

物件,描述要使用的搜尋和列舉組態。An object that describes the search and enumeration configuration to use.

傳回

String[]

在所指定目錄中,符合搜尋模式與列舉選項之子目錄完整名稱 (包含路徑) 的陣列,如果找不到任何目錄則為空陣列。An array of the full names (including paths) of the subdirectories that match the search pattern and enumeration options in the specified directory, or an empty array if no directories are found.

例外狀況

呼叫端沒有必要的權限。The caller does not have the required permission.

path 為零長度字串、只包含空格,或包含一或多個無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters. 您可以使用 GetInvalidPathChars() 查詢無效字元。You can query for invalid characters by using GetInvalidPathChars().

-或--or- searchPattern 不包含有效模式。searchPattern doesn't contain a valid pattern.

pathsearchPatternnullpath or searchPattern is null.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

path 為檔案名稱。path is a file name.

指定的路徑無效 (例如,它位於未對應的磁碟機上)。The specified path is invalid (for example, it is on an unmapped drive).

備註

這個方法會傳回符合指定搜尋模式之指定目錄底下的所有子目錄。This method returns all subdirectories directly under the specified directory that match the specified search pattern. 如果指定的目錄沒有子目錄,或沒有任何子目錄符合 searchPattern 參數,這個方法會傳回空陣列。If the specified directory has no subdirectories, or no subdirectories match the searchPattern parameter, this method returns an empty array. 只會搜尋最上層目錄。Only the top directory is searched. 如果您也想要搜尋子目錄,請使用方法, GetDirectories(String, String, SearchOption)AllDirectories 在參數中指定 searchOptionIf you want to search the subdirectories as well, use the GetDirectories(String, String, SearchOption) method and specify AllDirectories in the searchOption parameter.

searchPattern 可以是常值和萬用字元的組合,但是不支援正則運算式。searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. 中允許下列萬用字元規範 searchPatternThe following wildcard specifiers are permitted in searchPattern.

萬用字元規範Wildcard specifier 相符項Matches
* (星號) * (asterisk) 該位置中的零或多個字元。Zero or more characters in that position.
?? (問號)(question mark) 該位置中的零或一個字元。Zero or one character in that position.

萬用字元以外的字元是常值字元。Characters other than the wildcard are literal characters. 例如, searchPattern 字串 " * t" 會搜尋 path 結尾為字母 "t" 的所有名稱。For example, the searchPattern string "*t" searches for all names in path ending with the letter "t". searchPattern字串 "s * " 會搜尋開頭為 path 字母 "s" 的所有名稱。The searchPattern string "s*" searches for all names in path beginning with the letter "s".

searchPattern 無法在兩個期間結束 ( "...") 或包含兩個句點 ( "...") 後面接著 DirectorySeparatorCharAltDirectorySeparatorChar ,也不可以包含任何不正確字元。searchPattern cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any invalid characters. 您可以使用 GetInvalidPathChars 方法查詢無效字元。You can query for invalid characters by using the GetInvalidPathChars method.

path參數可以指定相對或絕對路徑資訊,但不區分大小寫。The path parameter can specify relative or absolute path information, and is not case-sensitive. 相對路徑資訊會解讀為相對於目前工作目錄。Relative path information is interpreted as relative to the current working directory. 若要取得目前的工作目錄,請參閱 GetCurrentDirectoryTo obtain the current working directory, see GetCurrentDirectory.

EnumerateDirectoriesGetDirectories 方法的差異如下:當您使用時 EnumerateDirectories ,您可以在傳回整個集合之前,開始列舉名稱集合; 當您使用時 GetDirectories ,您必須等候完整的名稱陣列傳回,然後才能存取陣列。The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array. 因此,當您使用許多檔案和目錄時, EnumerateDirectories 可能會更有效率。Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

如需一般 i/o 工作的清單,請參閱 一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

GetDirectories(String)

傳回指定之目錄中的子目錄名稱 (包括其路徑)。Returns the names of subdirectories (including their paths) in the specified directory.

public:
 static cli::array <System::String ^> ^ GetDirectories(System::String ^ path);
public static string[] GetDirectories (string path);
static member GetDirectories : string -> string[]
Public Shared Function GetDirectories (path As String) As String()

參數

path
String

要搜尋之目錄的相對或絕對路徑。The relative or absolute path to the directory to search. 這個字串不會區分大小寫。This string is not case-sensitive.

傳回

String[]

指定之路徑中的子目錄完整名稱 (包括路徑) 陣列,如果找不到任何目錄則為空陣列。An array of the full names (including paths) of subdirectories in the specified path, or an empty array if no directories are found.

例外狀況

呼叫端沒有必要的權限。The caller does not have the required permission.

path 為零長度字串、只包含空格,或包含一或多個無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters. 您可以使用 GetInvalidPathChars() 方法查詢無效字元。You can query for invalid characters by using the GetInvalidPathChars() method.

pathnullpath is null.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

path 為檔案名稱。path is a file name.

指定的路徑無效 (例如,它位於未對應的磁碟機上)。The specified path is invalid (for example, it is on an unmapped drive).

範例

下列範例會在命令列上接受檔案或目錄名稱的陣列、判斷其名稱類型,並適當地處理它。The following example takes an array of file or directory names on the command line, determines what kind of name it is, and processes it appropriately.

// For Directory::GetFiles and Directory::GetDirectories
// For File::Exists, Directory::Exists
using namespace System;
using namespace System::IO;
using namespace System::Collections;

// Insert logic for processing found files here.
void ProcessFile( String^ path )
{
   Console::WriteLine( "Processed file '{0}'.", path );
}


// Process all files in the directory passed in, recurse on any directories 
// that are found, and process the files they contain.
void ProcessDirectory( String^ targetDirectory )
{
   
   // Process the list of files found in the directory.
   array<String^>^fileEntries = Directory::GetFiles( targetDirectory );
   IEnumerator^ files = fileEntries->GetEnumerator();
   while ( files->MoveNext() )
   {
      String^ fileName = safe_cast<String^>(files->Current);
      ProcessFile( fileName );
   }

   
   // Recurse into subdirectories of this directory.
   array<String^>^subdirectoryEntries = Directory::GetDirectories( targetDirectory );
   IEnumerator^ dirs = subdirectoryEntries->GetEnumerator();
   while ( dirs->MoveNext() )
   {
      String^ subdirectory = safe_cast<String^>(dirs->Current);
      ProcessDirectory( subdirectory );
   }
}

int main( int argc, char *argv[] )
{
   for ( int i = 1; i < argc; i++ )
   {
      String^ path = gcnew String(argv[ i ]);
      if ( File::Exists( path ) )
      {
         
         // This path is a file
         ProcessFile( path );
      }
      else
      if ( Directory::Exists( path ) )
      {
         
         // This path is a directory
         ProcessDirectory( path );
      }
      else
      {
         Console::WriteLine( "{0} is not a valid file or directory.", path );
      }

   }
}

// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor
{
    public static void Main(string[] args)
    {
        foreach(string path in args)
        {
            if(File.Exists(path))
            {
                // This path is a file
                ProcessFile(path);
            }
            else if(Directory.Exists(path))
            {
                // This path is a directory
                ProcessDirectory(path);
            }
            else
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }
        }
    }

    // Process all files in the directory passed in, recurse on any directories
    // that are found, and process the files they contain.
    public static void ProcessDirectory(string targetDirectory)
    {
        // Process the list of files found in the directory.
        string [] fileEntries = Directory.GetFiles(targetDirectory);
        foreach(string fileName in fileEntries)
            ProcessFile(fileName);

        // Recurse into subdirectories of this directory.
        string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach(string subdirectory in subdirectoryEntries)
            ProcessDirectory(subdirectory);
    }

    // Insert logic for processing found files here.
    public static void ProcessFile(string path)
    {
        Console.WriteLine("Processed file '{0}'.", path);	
    }
}
' For Directory.GetFiles and Directory.GetDirectories
' For File.Exists, Directory.Exists 

Imports System.IO
Imports System.Collections

Public Class RecursiveFileProcessor

    Public Overloads Shared Sub Main(ByVal args() As String)
        Dim path As String
        For Each path In args
            If File.Exists(path) Then
                ' This path is a file.
                ProcessFile(path)
            Else
                If Directory.Exists(path) Then
                    ' This path is a directory.
                    ProcessDirectory(path)
                Else
                    Console.WriteLine("{0} is not a valid file or directory.", path)
                End If
            End If
        Next path
    End Sub


    ' Process all files in the directory passed in, recurse on any directories 
    ' that are found, and process the files they contain.
    Public Shared Sub ProcessDirectory(ByVal targetDirectory As String)
        Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
        ' Process the list of files found in the directory.
        Dim fileName As String
        For Each fileName In fileEntries
            ProcessFile(fileName)

        Next fileName
        Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
        ' Recurse into subdirectories of this directory.
        Dim subdirectory As String
        For Each subdirectory In subdirectoryEntries
            ProcessDirectory(subdirectory)
        Next subdirectory

    End Sub

    ' Insert logic for processing found files here.
    Public Shared Sub ProcessFile(ByVal path As String)
        Console.WriteLine("Processed file '{0}'.", path)
    End Sub
End Class

備註

這個方法與 GetDirectories(String, String) * 指定為搜尋模式的星號 () 相同,因此它會傳回所有的子目錄。This method is identical to GetDirectories(String, String) with the asterisk (*) specified as the search pattern, so it returns all subdirectories. 如果您需要搜尋子目錄,請使用 GetDirectories(String, String, SearchOption) 方法,它可讓您指定使用參數來搜尋子目錄 searchOptionIf you need to search subdirectories, use the GetDirectories(String, String, SearchOption) method, which enables you to specify a search of subdirectories with the searchOption parameter.

EnumerateDirectoriesGetDirectories 方法的差異如下:當您使用時 EnumerateDirectories ,您可以在傳回整個集合之前,開始列舉名稱集合; 當您使用時 GetDirectories ,您必須等候完整的名稱陣列傳回,然後才能存取陣列。The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array. 因此,當您使用許多檔案和目錄時, EnumerateDirectories 可能會更有效率。Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

path參數可以指定相對或絕對路徑資訊。The path parameter can specify relative or absolute path information. 相對路徑資訊會解讀為相對於目前工作目錄。Relative path information is interpreted as relative to the current working directory. 若要取得目前的工作目錄,請參閱 GetCurrentDirectoryTo obtain the current working directory, see GetCurrentDirectory.

這個方法所傳回的名稱前面會加上所提供的目錄資訊 pathThe names returned by this method are prefixed with the directory information provided in path.

path參數不區分大小寫。The path parameter is not case-sensitive.

如需一般 i/o 工作的清單,請參閱 一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

GetDirectories(String, String)

傳回指定目錄中符合指定搜尋模式的子目錄名稱 (包括檔案的路徑)。Returns the names of subdirectories (including their paths) that match the specified search pattern in the specified directory.

public:
 static cli::array <System::String ^> ^ GetDirectories(System::String ^ path, System::String ^ searchPattern);
public static string[] GetDirectories (string path, string searchPattern);
static member GetDirectories : string * string -> string[]
Public Shared Function GetDirectories (path As String, searchPattern As String) As String()

參數

path
String

要搜尋之目錄的相對或絕對路徑。The relative or absolute path to the directory to search. 這個字串不會區分大小寫。This string is not case-sensitive.

searchPattern
String

要比對 path 中子目錄名稱的搜尋字串。The search string to match against the names of subdirectories in path. 這個參數可以包含有效常值與萬用字元的組合,但是不支援規則運算式。This parameter can contain a combination of valid literal and wildcard characters, but it doesn't support regular expressions.

傳回

String[]

指定目錄中,符合搜尋模式的子目錄完整名稱 (包括路徑) 陣列,如果找不到任何目錄則為空陣列。An array of the full names (including paths) of the subdirectories that match the search pattern in the specified directory, or an empty array if no directories are found.

例外狀況

呼叫端沒有必要的權限。The caller does not have the required permission.

path 為零長度字串、只包含空格,或包含一或多個無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters. 您可以使用 GetInvalidPathChars() 查詢無效字元。You can query for invalid characters by using GetInvalidPathChars().

-或--or- searchPattern 不包含有效模式。searchPattern doesn't contain a valid pattern.

pathsearchPatternnullpath or searchPattern is null.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

path 為檔案名稱。path is a file name.

指定的路徑無效 (例如,它位於未對應的磁碟機上)。The specified path is invalid (for example, it is on an unmapped drive).

範例

下列範例會計算路徑中以指定字母開頭的目錄數目。The following example counts the number of directories in a path that begin with the specified letter.

using namespace System;
using namespace System::IO;
int main()
{
   try
   {
      
      // Only get subdirectories that begin with the letter "p."
      array<String^>^dirs = Directory::GetDirectories( "c:\\", "p*" );
      Console::WriteLine( "The number of directories starting with p is {0}.", dirs->Length );
      Collections::IEnumerator^ myEnum = dirs->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Console::WriteLine( myEnum->Current );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }

}

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            // Only get subdirectories that begin with the letter "p."
            string[] dirs = Directory.GetDirectories(@"c:\", "p*");
            Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length);
            foreach (string dir in dirs)
            {
                Console.WriteLine(dir);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Try
            ' Only get subdirectories that begin with the letter "p."
            Dim dirs As String() = Directory.GetDirectories("c:\", "p*")
            Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length)
            Dim dir As String
            For Each dir In dirs
                Console.WriteLine(dir)
            Next
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

備註

這個方法會傳回符合指定搜尋模式之指定目錄底下的所有子目錄。This method returns all subdirectories directly under the specified directory that match the specified search pattern. 如果指定的目錄沒有子目錄,或沒有任何子目錄符合 searchPattern 參數,這個方法會傳回空陣列。If the specified directory has no subdirectories, or no subdirectories match the searchPattern parameter, this method returns an empty array. 只會搜尋最上層目錄。Only the top directory is searched. 如果您也想要搜尋子目錄,請使用方法, GetDirectories(String, String, SearchOption)AllDirectories 在參數中指定 searchOptionIf you want to search the subdirectories as well, use the GetDirectories(String, String, SearchOption) method and specify AllDirectories in the searchOption parameter.

searchPattern 可以是常值和萬用字元的組合,但是不支援正則運算式。searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. 中允許下列萬用字元規範 searchPatternThe following wildcard specifiers are permitted in searchPattern.

萬用字元規範Wildcard specifier 相符項Matches
* (星號) * (asterisk) 該位置中的零或多個字元。Zero or more characters in that position.
?? (問號)(question mark) 該位置中的零或一個字元。Zero or one character in that position.

萬用字元以外的字元是常值字元。Characters other than the wildcard are literal characters. 例如, searchPattern 字串 " * t" 會搜尋 path 結尾為字母 "t" 的所有名稱。For example, the searchPattern string "*t" searches for all names in path ending with the letter "t". searchPattern字串 "s * " 會搜尋開頭為 path 字母 "s" 的所有名稱。The searchPattern string "s*" searches for all names in path beginning with the letter "s".

searchPattern 無法在兩個期間結束 ( "...") 或包含兩個句點 ( "...") 後面接著 DirectorySeparatorCharAltDirectorySeparatorChar ,也不可以包含任何不正確字元。searchPattern cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any invalid characters. 您可以使用 GetInvalidPathChars 方法查詢無效字元。You can query for invalid characters by using the GetInvalidPathChars method.

path參數可以指定相對或絕對路徑資訊,但不區分大小寫。The path parameter can specify relative or absolute path information, and is not case-sensitive. 相對路徑資訊會解讀為相對於目前工作目錄。Relative path information is interpreted as relative to the current working directory. 若要取得目前的工作目錄,請參閱 GetCurrentDirectoryTo obtain the current working directory, see GetCurrentDirectory.

EnumerateDirectoriesGetDirectories 方法的差異如下:當您使用時 EnumerateDirectories ,您可以在傳回整個集合之前,開始列舉名稱集合; 當您使用時 GetDirectories ,您必須等候完整的名稱陣列傳回,然後才能存取陣列。The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array. 因此,當您使用許多檔案和目錄時, EnumerateDirectories 可能會更有效率。Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

如需一般 i/o 工作的清單,請參閱 一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

適用於