DirectoryInfo.EnumerateFiles DirectoryInfo.EnumerateFiles DirectoryInfo.EnumerateFiles DirectoryInfo.EnumerateFiles Method

定義

傳回目前目錄中檔案資訊的可列舉集合。Returns an enumerable collection of file information in the current directory.

多載

EnumerateFiles() EnumerateFiles() EnumerateFiles() EnumerateFiles()

傳回目前目錄中檔案資訊的可列舉集合。Returns an enumerable collection of file information in the current directory.

EnumerateFiles(String) EnumerateFiles(String) EnumerateFiles(String) EnumerateFiles(String)

傳回符合指定之搜尋模式的檔案資訊的可列舉集合。Returns an enumerable collection of file information that matches a search pattern.

EnumerateFiles(String, EnumerationOptions) EnumerateFiles(String, EnumerationOptions) EnumerateFiles(String, EnumerationOptions)
EnumerateFiles(String, SearchOption) EnumerateFiles(String, SearchOption) EnumerateFiles(String, SearchOption)

傳回符合指定之搜尋模式和搜尋子目錄選項的檔案資訊的可列舉集合。Returns an enumerable collection of file information that matches a specified search pattern and search subdirectory option.

EnumerateFiles() EnumerateFiles() EnumerateFiles() EnumerateFiles()

傳回目前目錄中檔案資訊的可列舉集合。Returns an enumerable collection of file information in the current directory.

public:
 System::Collections::Generic::IEnumerable<System::IO::FileInfo ^> ^ EnumerateFiles();
public System.Collections.Generic.IEnumerable<System.IO.FileInfo> EnumerateFiles ();
member this.EnumerateFiles : unit -> seq<System.IO.FileInfo>
Public Function EnumerateFiles () As IEnumerable(Of FileInfo)

傳回

目前目錄中檔案的可列舉集合。An enumerable collection of the files in the current directory.

例外狀況

封裝在 DirectoryInfo 物件中的路徑無效 (例如,位於未對應的磁碟機上)。The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

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

範例

下列範例會列舉指定目錄下的檔案,並使用 LINQ 查詢,藉由檢查CreationTimeUtc屬性的值來傳回2009之前建立的所有檔案名。The following example enumerates the files under a specified directory and uses a LINQ query to return the names of all files that were created before 2009 by checking the value of the CreationTimeUtc property.

如果您只需要檔案的名稱,請使用靜態Directory類別以獲得更好的效能。If you only need the names of the files, use the static Directory class for better performance. 如需範例,請參閱Directory.EnumerateFiles(String)方法。For an example, see the Directory.EnumerateFiles(String) method.

// Create a DirectoryInfo of the directory of the files to enumerate.
DirectoryInfo DirInfo = new DirectoryInfo(@"\\archives1\library\");

DateTime StartOf2009 = new DateTime(2009, 01, 01);

// LINQ query for all files created before 2009.
var files = from f in DirInfo.EnumerateFiles()
           where f.CreationTimeUtc < StartOf2009
           select f;

// Show results.
foreach (var f in files)
{
    Console.WriteLine("{0}", f.Name);
}
' Create a DirectoryInfo of the directory of the files to enumerate.
Dim DirInfo As New DirectoryInfo("\\archives1\library\")

Dim StartOf2009 As New DateTime(2009, 1, 1)

' LINQ query for all files created before 2009.
Dim files = From f In DirInfo.EnumerateFiles()
            Where f.CreationTimeUtc < StartOf2009

' Show results.
For Each f As FileInfo In files
    Console.WriteLine("{0}", f.Name)
Next

下列範例顯示如何使用不同的搜尋選項來列舉目錄中的檔案。The following example shows how to enumerate files in 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:\ExampleDir");
            Console.WriteLine("No search pattern returns:");
            foreach (var fi in di.EnumerateFiles())
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

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

            Console.WriteLine();

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

            Console.WriteLine();

            Console.WriteLine("Search pattern AllDirectories returns:");
            foreach (var fi in di.EnumerateFiles("*", 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.EnumerateFiles()
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

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

        Console.WriteLine()

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

        Console.WriteLine()

        Console.WriteLine("Search pattern AllDirectories returns:")
        For Each fi In di.EnumerateFiles("*", 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.

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

傳回的集合不會進行快取;對集合上方法GetEnumerator的每個呼叫都會啟動新的列舉。The returned collection is not cached; each call to the GetEnumerator method on the collection will start a new enumeration.

EnumerateFiles(String) EnumerateFiles(String) EnumerateFiles(String) EnumerateFiles(String)

傳回符合指定之搜尋模式的檔案資訊的可列舉集合。Returns an enumerable collection of file information that matches a search pattern.

public:
 System::Collections::Generic::IEnumerable<System::IO::FileInfo ^> ^ EnumerateFiles(System::String ^ searchPattern);
public System.Collections.Generic.IEnumerable<System.IO.FileInfo> EnumerateFiles (string searchPattern);
member this.EnumerateFiles : string -> seq<System.IO.FileInfo>
Public Function EnumerateFiles (searchPattern As String) As IEnumerable(Of FileInfo)

參數

searchPattern
String String String 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.

傳回

傳回符合 searchPattern 的檔案的可列舉集合。An enumerable collection of files that matches searchPattern.

例外狀況

封裝在 DirectoryInfo 物件中的路徑無效 (例如,位於未對應的磁碟機上)。The path encapsulated in the DirectoryInfo object is invalid, (for example, it is on an unmapped drive).

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

範例

下列範例顯示如何使用不同的搜尋選項來列舉目錄中的檔案。The following example shows how to enumerate files in 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:\ExampleDir");
            Console.WriteLine("No search pattern returns:");
            foreach (var fi in di.EnumerateFiles())
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

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

            Console.WriteLine();

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

            Console.WriteLine();

            Console.WriteLine("Search pattern AllDirectories returns:");
            foreach (var fi in di.EnumerateFiles("*", 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.EnumerateFiles()
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

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

        Console.WriteLine()

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

        Console.WriteLine()

        Console.WriteLine("Search pattern AllDirectories returns:")
        For Each fi In di.EnumerateFiles("*", 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". ".". 字串 "s*" 會path搜尋開頭為字母 "s" 的所有名稱。 searchPatternThe 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.

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

傳回的集合不會進行快取;對集合上方法GetEnumerator的每個呼叫都會啟動新的列舉。The returned collection is not cached; each call to the GetEnumerator method on the collection will start a new enumeration.

EnumerateFiles(String, EnumerationOptions) EnumerateFiles(String, EnumerationOptions) EnumerateFiles(String, EnumerationOptions)

public:
 System::Collections::Generic::IEnumerable<System::IO::FileInfo ^> ^ EnumerateFiles(System::String ^ searchPattern, System::IO::EnumerationOptions ^ enumerationOptions);
public System.Collections.Generic.IEnumerable<System.IO.FileInfo> EnumerateFiles (string searchPattern, System.IO.EnumerationOptions enumerationOptions);
member this.EnumerateFiles : string * System.IO.EnumerationOptions -> seq<System.IO.FileInfo>

參數

searchPattern
String String String String

傳回

EnumerateFiles(String, SearchOption) EnumerateFiles(String, SearchOption) EnumerateFiles(String, SearchOption)

傳回符合指定之搜尋模式和搜尋子目錄選項的檔案資訊的可列舉集合。Returns an enumerable collection of file information that matches a specified search pattern and search subdirectory option.

public:
 System::Collections::Generic::IEnumerable<System::IO::FileInfo ^> ^ EnumerateFiles(System::String ^ searchPattern, System::IO::SearchOption searchOption);
public System.Collections.Generic.IEnumerable<System.IO.FileInfo> EnumerateFiles (string searchPattern, System.IO.SearchOption searchOption);
member this.EnumerateFiles : string * System.IO.SearchOption -> seq<System.IO.FileInfo>

參數

searchPattern
String String String 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 SearchOption SearchOption SearchOption

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

傳回

傳回符合 searchPatternsearchOption 的檔案的可列舉集合。An enumerable collection of files that matches searchPattern and searchOption.

例外狀況

封裝在 DirectoryInfo 物件中的路徑無效 (例如,位於未對應的磁碟機上)。The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

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

範例

下列範例顯示如何使用不同的搜尋選項來列舉目錄中的檔案。The following example shows how to enumerate files in 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:\ExampleDir");
            Console.WriteLine("No search pattern returns:");
            foreach (var fi in di.EnumerateFiles())
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

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

            Console.WriteLine();

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

            Console.WriteLine();

            Console.WriteLine("Search pattern AllDirectories returns:");
            foreach (var fi in di.EnumerateFiles("*", 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.EnumerateFiles()
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

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

        Console.WriteLine()

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

        Console.WriteLine()

        Console.WriteLine("Search pattern AllDirectories returns:")
        For Each fi In di.EnumerateFiles("*", 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". ".". 字串 "s*" 會path搜尋開頭為字母 "s" 的所有名稱。 searchPatternThe 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.

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

傳回的集合不會進行快取;對集合上方法GetEnumerator的每個呼叫都會啟動新的列舉。The returned collection is not cached; each call to the GetEnumerator method on the collection will start a new enumeration.

適用於