DirectoryInfo.EnumerateFiles 方法

定义

返回当前目录中的文件信息的可枚举集合。Returns an enumerable collection of file information in the current directory.

重载

EnumerateFiles()

返回当前目录中的文件信息的可枚举集合。Returns an enumerable collection of file information in the current directory.

EnumerateFiles(String)

返回与搜索模式匹配的文件信息的可枚举集合。Returns an enumerable collection of file information that matches a search pattern.

EnumerateFiles(String, EnumerationOptions)

返回与指定的搜索模式和枚举选项匹配的文件信息的可枚举集合。Returns an enumerable collection of file information that matches the specified search pattern and enumeration options.

EnumerateFiles(String, SearchOption)

返回与指定的搜索模式和搜索子目录选项匹配的文件信息的可枚举集合。Returns an enumerable collection of file information that matches a specified search pattern and search subdirectory option.

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 查询返回在2009之前创建的所有文件的名称,方法是检查 CreationTimeUtc 属性的值。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.txt、log2、test1、test2、test3 和包含名为子文件的文件的子目录。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)

返回与搜索模式匹配的文件信息的可枚举集合。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

要与文件名匹配的搜索字符串。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.

异常

searchPatternnullsearchPattern is null.

封装在 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.txt、log2、test1、test2、test3 和包含名为子文件的文件的子目录。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". "。". 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.

此方法预先填充以下 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)

返回与指定的搜索模式和枚举选项匹配的文件信息的可枚举集合。Returns an enumerable collection of file information that matches the specified search pattern and enumeration options.

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

要与文件名匹配的搜索字符串。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 匹配的文件的可枚举集合。An enumerable collection of files that matches searchPattern and enumerationOptions.

异常

searchPatternnullsearchPattern is null.

封装在 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.

注解

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.

此方法预先填充以下 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, 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

要与文件名匹配的搜索字符串。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. 默认值是 TopDirectoryOnlyThe default value is TopDirectoryOnly.

返回

searchPatternsearchOption 匹配的文件的可枚举集合。An enumerable collection of files that matches searchPattern and searchOption.

异常

searchPatternnullsearchPattern is null.

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

封装在 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.txt、log2、test1、test2、test3 和包含名为子文件的文件的子目录。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". "。". 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.

此方法预先填充以下 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.

适用于