DirectoryInfo.GetFiles 方法

定義

從目前的目錄傳回檔案清單。Returns a file list from the current directory.

多載

GetFiles(String, EnumerationOptions)

傳回目前目錄中符合所指定搜尋模式和列舉選項的檔案清單。Returns a file list from the current directory matching the specified search pattern and enumeration options.

GetFiles(String, SearchOption)

從目前目錄傳回符合指定搜尋模式的檔案清單,並使用值來判斷是否搜尋子目錄。Returns a file list from the current directory matching the given search pattern and using a value to determine whether to search subdirectories.

GetFiles()

從目前的目錄傳回檔案清單。Returns a file list from the current directory.

GetFiles(String)

從目前目錄傳回符合指定之搜尋模式的檔案清單。Returns a file list from the current directory matching the given search pattern.

GetFiles(String, EnumerationOptions)

傳回目前目錄中符合所指定搜尋模式和列舉選項的檔案清單。Returns a file list from the current directory matching the specified search pattern and enumeration options.

public:
 cli::array <System::IO::FileInfo ^> ^ GetFiles(System::String ^ searchPattern, System::IO::EnumerationOptions ^ enumerationOptions);
public System.IO.FileInfo[] GetFiles (string searchPattern, System.IO.EnumerationOptions enumerationOptions);
member this.GetFiles : string * System.IO.EnumerationOptions -> System.IO.FileInfo[]

參數

searchPattern
String

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

enumerationOptions
EnumerationOptions

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

傳回

符合 searchPatternenumerationOptions 的強型別 FileInfo 物件陣列。An array of strongly typed FileInfo objects that match searchPattern and enumerationOptions.

例外狀況

searchPattern 包含一或多個 GetInvalidPathChars() 方法所定義的無效字元。searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPatternnullsearchPattern is null.

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

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

備註

searchPattern 可以是常值和萬用字元的組合,但是不支援正則運算式。searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. searchPattern中允許下列萬用字元規範。The 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. 例如,字串 "*t" 會搜尋結尾為字母 "t" 的所有名稱。For example, the string "*t" searches for all names in ending with the letter "t". searchPattern 字串 "s*" 會搜尋 path 開頭為字母 "s" 的所有名稱。The searchPattern string "s*" searches for all names in path beginning with the letter "s".

EnumerateFilesGetFiles 方法的差異如下:The EnumerateFiles and GetFiles methods differ as follows:

  • 當您使用 EnumerateFiles時,可以在傳回整個集合之前,先開始列舉 FileInfo 物件的集合。When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

  • 當您使用 GetFiles時,您必須等候 FileInfo 物件的整個陣列傳回,然後才能存取陣列。When you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array.

因此,當您使用許多檔案和目錄時,EnumerateFiles 可能會更有效率。Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

如果 DirectoryInfo中沒有任何檔案,這個方法會傳回空陣列。If there are no files in the DirectoryInfo, this method returns an empty array.

searchPattern 參數中允許下列萬用字元規範。The following wildcard specifiers are permitted in the searchPattern parameter.

萬用字元Wildcard character 描述Description
* 零或多個字元。Zero or more characters.
?? 正好零或一個字元。Exactly zero or one character.

不保證所傳回檔案名的順序;如果需要特定的排序次序,請使用 Sort 方法。The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

允許使用萬用字元。Wildcards are permitted. 例如,searchPattern 字串 "* .txt" 會搜尋副檔名為 "txt" 的所有檔案名。For example, the searchPattern string "*.txt" searches for all file names having an extension of "txt". searchPattern 字串 "s*" 會搜尋開頭為字母 "s" 的所有檔案名。The searchPattern string "s*" searches for all file names beginning with the letter "s". 如果沒有檔案,或沒有任何檔案符合 DirectoryInfo中的 searchPattern 字串,這個方法會傳回空陣列。If there are no files, or no files that match the searchPattern string in the DirectoryInfo, this method returns an empty array.

注意

searchPattern 中使用星號萬用字元(例如 "* .txt")時,比對行為會根據指定之副檔名的長度而有所不同。When using the asterisk wildcard character in a searchPattern (for example, "*.txt"), the matching behavior varies depending on the length of the specified file extension. 副檔名只有三個字元的 searchPattern 會傳回副檔名為三個或更多字元的檔案,其中前三個字元符合 searchPattern中指定的副檔名。A searchPattern with a file extension of exactly three characters returns files with an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. 副檔名為一個、兩個或三個以上的 searchPattern,只會傳回副檔名剛好符合 searchPattern中指定之副檔名的檔案。A searchPattern with a file extension of one, two, or more than three characters returns only files with extensions of exactly that length that match the file extension specified in the searchPattern. 當使用問號萬用字元時,這個方法只會傳回符合指定副檔名的檔案。When using the question mark wildcard character, this method returns only files that match the specified file extension. 例如,假設目錄中有兩個檔案 "file1 .txt" 和 "file1. txtother",則為 "file?" 的搜尋模式。txt "只會傳回第一個檔案,而" file*.txt "的搜尋模式會傳回這兩個檔案。For example, given two files in a directory, "file1.txt" and "file1.txtother", a search pattern of "file?.txt" returns only the first file, while a search pattern of "file*.txt" returns both files.

注意

因為這個方法會檢查檔案名同時包含8.3 檔案名格式和完整檔案名格式,所以類似 "* 1*.txt" 的搜尋模式可能會傳回非預期的檔案名。Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. 例如,使用 "*1*.txt" 的搜尋模式會傳回 "longfilename",因為對等的8.3 檔案名格式會是 "longf ~ 1 .txt"。For example, using a search pattern of "*1*.txt" will return "longfilename.txt" because the equivalent 8.3 file name format would be "longf~1.txt".

這個方法會預先填入下列 FileInfo 屬性的值:This method pre-populates the values of the following FileInfo properties:

GetFiles(String, SearchOption)

從目前目錄傳回符合指定搜尋模式的檔案清單,並使用值來判斷是否搜尋子目錄。Returns a file list from the current directory matching the given search pattern and using a value to determine whether to search subdirectories.

public:
 cli::array <System::IO::FileInfo ^> ^ GetFiles(System::String ^ searchPattern, System::IO::SearchOption searchOption);
public System.IO.FileInfo[] GetFiles (string searchPattern, System.IO.SearchOption searchOption);
member this.GetFiles : string * System.IO.SearchOption -> System.IO.FileInfo[]

參數

searchPattern
String

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

searchOption
SearchOption

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

傳回

類型為 FileInfo 的陣列。An array of type FileInfo.

例外狀況

searchPattern 包含一或多個 GetInvalidPathChars() 方法所定義的無效字元。searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPatternnullsearchPattern is null.

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

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

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

範例

下列範例顯示如何使用不同的搜尋選項,從目錄取得檔案清單。The following example shows how to get a list of files from a directory by using different search options. 此範例假設有一個目錄,其中包含名為 log1 的檔案、log2 .txt、test1、test2 .txt、test3,以及一個檔案名為 SubFile .txt 的子目錄。The example assumes a directory that has files named log1.txt, log2.txt, test1.txt, test2.txt, test3.txt, and a subdirectory that has a file named SubFile.txt.

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(@"C:\Users\tomfitz\Documents\ExampleDir");
            Console.WriteLine("No search pattern returns:");
            foreach (var fi in di.GetFiles())
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern *2* returns:");
            foreach (var fi in di.GetFiles("*2*"))
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern test?.txt returns:");
            foreach (var fi in di.GetFiles("test?.txt"))
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern AllDirectories returns:");
            foreach (var fi in di.GetFiles("*", SearchOption.AllDirectories))
            {
                Console.WriteLine(fi.Name);
            }
        }
    }
}
/* 
This code produces output similar to the following:

No search pattern returns:
log1.txt
log2.txt
test1.txt
test2.txt
test3.txt

Search pattern *2* returns:
log2.txt
test2.txt

Search pattern test?.txt returns:
test1.txt
test2.txt
test3.txt

Search pattern AllDirectories returns:
log1.txt
log2.txt
test1.txt
test2.txt
test3.txt
SubFile.txt
Press any key to continue . . .

*/
Imports System.IO

Module Module1

    Sub Main()
        Dim di As DirectoryInfo = New DirectoryInfo("C:\ExampleDir")
        Console.WriteLine("No search pattern returns:")
        For Each fi In di.GetFiles()
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern *2* returns:")
        For Each fi In di.GetFiles("*2*")
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern test?.txt returns:")
        For Each fi In di.GetFiles("test?.txt")
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern AllDirectories returns:")
        For Each fi In di.GetFiles("*", SearchOption.AllDirectories)
            Console.WriteLine(fi.Name)
        Next
    End Sub

End Module

' This code produces output similar to the following:

' No search pattern returns:
' log1.txt
' log2.txt
' test1.txt
' test2.txt
' test3.txt

' Search pattern *2* returns:
' log2.txt
' test2.txt

' Search pattern test?.txt returns:
' test1.txt
' test2.txt
' test3.txt

' Search pattern AllDirectories returns:
' log1.txt
' log2.txt
' test1.txt
' test2.txt
' test3.txt
' SubFile.txt
' Press any key to continue . . .

備註

EnumerateFilesGetFiles 方法的差異如下:The EnumerateFiles and GetFiles methods differ as follows:

  • 當您使用 EnumerateFiles時,可以在傳回整個集合之前,先開始列舉 FileInfo 物件的集合。When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

  • 當您使用 GetFiles時,您必須等候 FileInfo 物件的整個陣列傳回,然後才能存取陣列。When you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array.

因此,當您使用許多檔案和目錄時,EnumerateFiles 可能會更有效率。Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

如果 DirectoryInfo中沒有任何檔案,這個方法會傳回空陣列。If there are no files in the DirectoryInfo, this method returns an empty array.

searchPattern中允許下列萬用字元規範。The following wildcard specifiers are permitted in searchPattern.

萬用字元Wildcard character 描述Description
* (星號)* (asterisk) 零或多個字元。Zero or more characters.
?? (問號)(question mark) 正好零或一個字元。Exactly zero or one character.

不保證所傳回檔案名的順序;如果需要特定的排序次序,請使用 Sort 方法。The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

允許使用萬用字元。Wildcards are permitted. 例如,searchPattern 字串 "* .txt" 會搜尋副檔名為 "txt" 的所有檔案名。For example, the searchPattern string "*.txt" searches for all file names having an extension of "txt". searchPattern 字串 "s*" 會搜尋開頭為字母 "s" 的所有檔案名。The searchPattern string "s*" searches for all file names beginning with the letter "s". 如果沒有檔案,或沒有任何檔案符合 DirectoryInfo中的 searchPattern 字串,這個方法會傳回空陣列。If there are no files, or no files that match the searchPattern string in the DirectoryInfo, this method returns an empty array.

注意

searchPattern 中使用星號萬用字元(例如 "* .txt")時,比對行為會根據指定之副檔名的長度而有所不同。When using the asterisk wildcard character in a searchPattern (for example, "*.txt"), the matching behavior varies depending on the length of the specified file extension. 副檔名只有三個字元的 searchPattern 會傳回副檔名為三個或更多字元的檔案,其中前三個字元符合 searchPattern中指定的副檔名。A searchPattern with a file extension of exactly three characters returns files with an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. 副檔名為一個、兩個或三個以上的 searchPattern,只會傳回副檔名剛好符合 searchPattern中指定之副檔名的檔案。A searchPattern with a file extension of one, two, or more than three characters returns only files with extensions of exactly that length that match the file extension specified in the searchPattern. 當使用問號萬用字元時,這個方法只會傳回符合指定副檔名的檔案。When using the question mark wildcard character, this method returns only files that match the specified file extension. 例如,假設目錄中有兩個檔案 "file1 .txt" 和 "file1. txtother",則為 "file?" 的搜尋模式。txt "只會傳回第一個檔案,而" file*.txt "的搜尋模式會傳回這兩個檔案。For example, given two files in a directory, "file1.txt" and "file1.txtother", a search pattern of "file?.txt" returns only the first file, while a search pattern of "file*.txt" returns both files.

下列清單顯示 searchPattern 參數的不同長度的行為:The following list shows the behavior of different lengths for the searchPattern parameter:

  • "* abc" 會傳回副檔名為 abc、. abcd、. abcde、. abcdef 等等的檔案。"*.abc" returns files having an extension of.abc,.abcd,.abcde,.abcdef, and so on.

  • "* abcd" 只會傳回副檔名為 abcd 的檔案。"*.abcd" returns only files having an extension of.abcd.

  • "*. abcde" 只會傳回副檔名為 abcde 的檔案。"*.abcde" returns only files having an extension of.abcde.

  • "*. abcdef" 只會傳回副檔名為 abcdef 的檔案。"*.abcdef" returns only files having an extension of.abcdef.

注意

因為這個方法會檢查檔案名同時包含8.3 檔案名格式和完整檔案名格式,所以類似 "* 1*.txt" 的搜尋模式可能會傳回非預期的檔案名。Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. 例如,使用 "*1*.txt" 的搜尋模式會傳回 "longfilename",因為對等的8.3 檔案名格式會是 "longf ~ 1 .txt"。For example, using a search pattern of "*1*.txt" will return "longfilename.txt" because the equivalent 8.3 file name format would be "longf~1.txt".

這個方法會預先填入下列 FileInfo 屬性的值:This method pre-populates the values of the following FileInfo properties:

  1. Attributes

  2. CreationTime

  3. CreationTimeUtc

  4. LastAccessTime

  5. LastAccessTimeUtc

  6. LastWriteTime

  7. LastWriteTimeUtc

  8. Length

安全性

FileIOPermission
用於存取路徑和讀取檔案和目錄。for access to the path and for reading from files and directories. 相關聯的列舉: ReadPathDiscoveryAssociated enumerations: Read, PathDiscovery

另請參閱

GetFiles()

從目前的目錄傳回檔案清單。Returns a file list from the current directory.

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

傳回

類型為 FileInfo 的陣列。An array of type FileInfo.

例外狀況

路徑無效,例如位於未對應的磁碟機上。The path is invalid, such as being on an unmapped drive.

範例

下列範例顯示如何使用不同的搜尋選項,從目錄取得檔案清單。The following example shows how to get a list of files from a directory by using different search options. 此範例假設有一個目錄,其中包含名為 log1 的檔案、log2 .txt、test1、test2 .txt、test3,以及一個檔案名為 SubFile .txt 的子目錄。The example assumes a directory that has files named log1.txt, log2.txt, test1.txt, test2.txt, test3.txt, and a subdirectory that has a file named SubFile.txt.

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(@"C:\Users\tomfitz\Documents\ExampleDir");
            Console.WriteLine("No search pattern returns:");
            foreach (var fi in di.GetFiles())
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern *2* returns:");
            foreach (var fi in di.GetFiles("*2*"))
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern test?.txt returns:");
            foreach (var fi in di.GetFiles("test?.txt"))
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern AllDirectories returns:");
            foreach (var fi in di.GetFiles("*", SearchOption.AllDirectories))
            {
                Console.WriteLine(fi.Name);
            }
        }
    }
}
/* 
This code produces output similar to the following:

No search pattern returns:
log1.txt
log2.txt
test1.txt
test2.txt
test3.txt

Search pattern *2* returns:
log2.txt
test2.txt

Search pattern test?.txt returns:
test1.txt
test2.txt
test3.txt

Search pattern AllDirectories returns:
log1.txt
log2.txt
test1.txt
test2.txt
test3.txt
SubFile.txt
Press any key to continue . . .

*/
Imports System.IO

Module Module1

    Sub Main()
        Dim di As DirectoryInfo = New DirectoryInfo("C:\ExampleDir")
        Console.WriteLine("No search pattern returns:")
        For Each fi In di.GetFiles()
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern *2* returns:")
        For Each fi In di.GetFiles("*2*")
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern test?.txt returns:")
        For Each fi In di.GetFiles("test?.txt")
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern AllDirectories returns:")
        For Each fi In di.GetFiles("*", SearchOption.AllDirectories)
            Console.WriteLine(fi.Name)
        Next
    End Sub

End Module

' This code produces output similar to the following:

' No search pattern returns:
' log1.txt
' log2.txt
' test1.txt
' test2.txt
' test3.txt

' Search pattern *2* returns:
' log2.txt
' test2.txt

' Search pattern test?.txt returns:
' test1.txt
' test2.txt
' test3.txt

' Search pattern AllDirectories returns:
' log1.txt
' log2.txt
' test1.txt
' test2.txt
' test3.txt
' SubFile.txt
' Press any key to continue . . .

備註

EnumerateFilesGetFiles 方法的差異如下:The EnumerateFiles and GetFiles methods differ as follows:

  • 當您使用 EnumerateFiles時,可以在傳回整個集合之前,先開始列舉 FileInfo 物件的集合。When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

  • 當您使用 GetFiles時,您必須等候 FileInfo 物件的整個陣列傳回,然後才能存取陣列。When you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array.

因此,當您使用許多檔案和目錄時,EnumerateFiles 可能會更有效率。Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

如果 DirectoryInfo中沒有任何檔案,這個方法會傳回空陣列。If there are no files in the DirectoryInfo, this method returns an empty array.

不保證所傳回檔案名的順序;如果需要特定的排序次序,請使用 Sort 方法。The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

這個方法會預先填入下列 FileInfo 屬性的值:This method pre-populates the values of the following FileInfo properties:

安全性

FileIOPermission
用於讀取目錄。for reading directories. 相關聯的列舉: ReadAssociated enumeration: Read

另請參閱

GetFiles(String)

從目前目錄傳回符合指定之搜尋模式的檔案清單。Returns a file list from the current directory matching the given search pattern.

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

參數

searchPattern
String

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

傳回

類型為 FileInfo 的陣列。An array of type FileInfo.

例外狀況

searchPattern 包含一或多個 GetInvalidPathChars() 方法所定義的無效字元。searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPatternnullsearchPattern is null.

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

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

範例

下列範例顯示如何使用不同的搜尋選項,從目錄取得檔案清單。The following example shows how to get a list of files from a directory by using different search options. 此範例假設有一個目錄,其中包含名為 log1 的檔案、log2 .txt、test1、test2 .txt、test3,以及一個檔案名為 SubFile .txt 的子目錄。The example assumes a directory that has files named log1.txt, log2.txt, test1.txt, test2.txt, test3.txt, and a subdirectory that has a file named SubFile.txt.

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(@"C:\Users\tomfitz\Documents\ExampleDir");
            Console.WriteLine("No search pattern returns:");
            foreach (var fi in di.GetFiles())
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern *2* returns:");
            foreach (var fi in di.GetFiles("*2*"))
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern test?.txt returns:");
            foreach (var fi in di.GetFiles("test?.txt"))
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern AllDirectories returns:");
            foreach (var fi in di.GetFiles("*", SearchOption.AllDirectories))
            {
                Console.WriteLine(fi.Name);
            }
        }
    }
}
/* 
This code produces output similar to the following:

No search pattern returns:
log1.txt
log2.txt
test1.txt
test2.txt
test3.txt

Search pattern *2* returns:
log2.txt
test2.txt

Search pattern test?.txt returns:
test1.txt
test2.txt
test3.txt

Search pattern AllDirectories returns:
log1.txt
log2.txt
test1.txt
test2.txt
test3.txt
SubFile.txt
Press any key to continue . . .

*/
Imports System.IO

Module Module1

    Sub Main()
        Dim di As DirectoryInfo = New DirectoryInfo("C:\ExampleDir")
        Console.WriteLine("No search pattern returns:")
        For Each fi In di.GetFiles()
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern *2* returns:")
        For Each fi In di.GetFiles("*2*")
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern test?.txt returns:")
        For Each fi In di.GetFiles("test?.txt")
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern AllDirectories returns:")
        For Each fi In di.GetFiles("*", SearchOption.AllDirectories)
            Console.WriteLine(fi.Name)
        Next
    End Sub

End Module

' This code produces output similar to the following:

' No search pattern returns:
' log1.txt
' log2.txt
' test1.txt
' test2.txt
' test3.txt

' Search pattern *2* returns:
' log2.txt
' test2.txt

' Search pattern test?.txt returns:
' test1.txt
' test2.txt
' test3.txt

' Search pattern AllDirectories returns:
' log1.txt
' log2.txt
' test1.txt
' test2.txt
' test3.txt
' SubFile.txt
' Press any key to continue . . .

備註

searchPattern 可以是常值和萬用字元的組合,但是不支援正則運算式。searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. searchPattern中允許下列萬用字元規範。The 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. 例如,字串 "*t" 會搜尋結尾為字母 "t" 的所有名稱。For example, the string "*t" searches for all names in ending with the letter "t". searchPattern 字串 "s*" 會搜尋 path 開頭為字母 "s" 的所有名稱。The searchPattern string "s*" searches for all names in path beginning with the letter "s".

EnumerateFilesGetFiles 方法的差異如下:The EnumerateFiles and GetFiles methods differ as follows:

  • 當您使用 EnumerateFiles時,可以在傳回整個集合之前,先開始列舉 FileInfo 物件的集合。When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

  • 當您使用 GetFiles時,您必須等候 FileInfo 物件的整個陣列傳回,然後才能存取陣列。When you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array.

因此,當您使用許多檔案和目錄時,EnumerateFiles 可能會更有效率。Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

如果 DirectoryInfo中沒有任何檔案,這個方法會傳回空陣列。If there are no files in the DirectoryInfo, this method returns an empty array.

searchPattern 參數中允許下列萬用字元規範。The following wildcard specifiers are permitted in the searchPattern parameter.

萬用字元Wildcard character 描述Description
* 零或多個字元。Zero or more characters.
?? 正好零或一個字元。Exactly zero or one character.

不保證所傳回檔案名的順序;如果需要特定的排序次序,請使用 Sort 方法。The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

允許使用萬用字元。Wildcards are permitted. 例如,searchPattern 字串 "* .txt" 會搜尋副檔名為 "txt" 的所有檔案名。For example, the searchPattern string "*.txt" searches for all file names having an extension of "txt". searchPattern 字串 "s*" 會搜尋開頭為字母 "s" 的所有檔案名。The searchPattern string "s*" searches for all file names beginning with the letter "s". 如果沒有檔案,或沒有任何檔案符合 DirectoryInfo中的 searchPattern 字串,這個方法會傳回空陣列。If there are no files, or no files that match the searchPattern string in the DirectoryInfo, this method returns an empty array.

注意

searchPattern 中使用星號萬用字元(例如 "* .txt")時,比對行為會根據指定之副檔名的長度而有所不同。When using the asterisk wildcard character in a searchPattern (for example, "*.txt"), the matching behavior varies depending on the length of the specified file extension. 副檔名只有三個字元的 searchPattern 會傳回副檔名為三個或更多字元的檔案,其中前三個字元符合 searchPattern中指定的副檔名。A searchPattern with a file extension of exactly three characters returns files with an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. 副檔名為一個、兩個或三個以上的 searchPattern,只會傳回副檔名剛好符合 searchPattern中指定之副檔名的檔案。A searchPattern with a file extension of one, two, or more than three characters returns only files with extensions of exactly that length that match the file extension specified in the searchPattern. 當使用問號萬用字元時,這個方法只會傳回符合指定副檔名的檔案。When using the question mark wildcard character, this method returns only files that match the specified file extension. 例如,假設目錄中有兩個檔案 "file1 .txt" 和 "file1. txtother",則為 "file?" 的搜尋模式。txt "只會傳回第一個檔案,而" file*.txt "的搜尋模式會傳回這兩個檔案。For example, given two files in a directory, "file1.txt" and "file1.txtother", a search pattern of "file?.txt" returns only the first file, while a search pattern of "file*.txt" returns both files.

注意

因為這個方法會檢查檔案名同時包含8.3 檔案名格式和完整檔案名格式,所以類似 "* 1*.txt" 的搜尋模式可能會傳回非預期的檔案名。Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. 例如,使用 "*1*.txt" 的搜尋模式會傳回 "longfilename",因為對等的8.3 檔案名格式會是 "longf ~ 1 .txt"。For example, using a search pattern of "*1*.txt" will return "longfilename.txt" because the equivalent 8.3 file name format would be "longf~1.txt".

這個方法會預先填入下列 FileInfo 屬性的值:This method pre-populates the values of the following FileInfo properties:

安全性

FileIOPermission
用於存取路徑和讀取檔案和目錄。for access to the path and for reading from files and directories. 相關聯的列舉: ReadPathDiscoveryAssociated enumerations: Read, PathDiscovery

另請參閱

適用於