Directory.EnumerateFileSystemEntries 方法

定义

返回满足指定条件的文件系统项的可枚举集合。

重载

EnumerateFileSystemEntries(String)

返回指定路径中的文件名和目录名的可枚举集合。

EnumerateFileSystemEntries(String, String)

返回指定路径中与搜索模式匹配的文件名和目录名的可枚举集合。

EnumerateFileSystemEntries(String, String, EnumerationOptions)

返回指定路径中与搜索模式和枚举选项匹配的文件名和目录名的可枚举集合。

EnumerateFileSystemEntries(String, String, SearchOption)

返回指定路径中与搜索模式匹配的文件名称和目录名的可枚举集合,还可以搜索子目录。

EnumerateFileSystemEntries(String)

Source:
Directory.cs
Source:
Directory.cs
Source:
Directory.cs

返回指定路径中的文件名和目录名的可枚举集合。

public:
 static System::Collections::Generic::IEnumerable<System::String ^> ^ EnumerateFileSystemEntries(System::String ^ path);
public static System.Collections.Generic.IEnumerable<string> EnumerateFileSystemEntries (string path);
static member EnumerateFileSystemEntries : string -> seq<string>
Public Shared Function EnumerateFileSystemEntries (path As String) As IEnumerable(Of String)

参数

path
String

要搜索的目录的相对或绝对路径。 此字符串不区分大小写。

返回

path 指定的目录中的文件系统条目的可枚举集合。

例外

.NET Framework 和 2.1 之前的 .NET Core 版本:path是长度为零的字符串,仅包含空格或包含无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

pathnull

path 无效,如引用未映射的驱动器。

path 是一个文件名。

指定的路径和/或文件名超过了系统定义的最大长度。

调用方没有所要求的权限。

调用方没有所要求的权限。

注解

可以使用 参数指定相对路径信息 path 。 相对路径信息解释为相对于当前工作目录的信息,可以使用 方法确定 GetCurrentDirectory 该目录。

EnumerateFileSystemEntriesGetFileSystemEntries 方法的不同之处如下:使用 EnumerateFileSystemEntries时,可以在返回整个集合之前开始枚举条目集合;使用 GetFileSystemEntries时,必须等待整个条目数组返回,然后才能访问数组。 因此,在使用许多文件和目录时, EnumerateFileSystemEntries 可以更高效。

未缓存返回的集合;每次调用 GetEnumerator 集合上的 将启动一个新的枚举。

适用于

EnumerateFileSystemEntries(String, String)

Source:
Directory.cs
Source:
Directory.cs
Source:
Directory.cs

返回指定路径中与搜索模式匹配的文件名和目录名的可枚举集合。

public:
 static System::Collections::Generic::IEnumerable<System::String ^> ^ EnumerateFileSystemEntries(System::String ^ path, System::String ^ searchPattern);
public static System.Collections.Generic.IEnumerable<string> EnumerateFileSystemEntries (string path, string searchPattern);
static member EnumerateFileSystemEntries : string * string -> seq<string>
Public Shared Function EnumerateFileSystemEntries (path As String, searchPattern As String) As IEnumerable(Of String)

参数

path
String

要搜索的目录的相对或绝对路径。 此字符串不区分大小写。

searchPattern
String

要与 path 中的文件系统条目的名称进行匹配的搜索字符串。 此参数可以包含有效文本路径和通配符(* 和 ?)的组合,但不支持正则表达式。

返回

path 指定且与指定的搜索模式匹配的目录中的文件系统条目的可枚举集合。

例外

.NET Framework 和 2.1 之前的 .NET Core 版本:path是长度为零的字符串,仅包含空格或包含无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

- 或 -

searchPattern 不包含有效模式。

pathnull

searchPatternnull

path 无效,如引用未映射的驱动器。

path 是一个文件名。

指定的路径和/或文件名超过了系统定义的最大长度。

调用方没有所要求的权限。

调用方没有所要求的权限。

注解

searchPattern 可以是文本和通配符的组合,但它不支持正则表达式。 允许在 中 searchPattern使用以下通配符说明符。

通配符说明符 匹配
*(星号) 该位置的零个或多个字符。
? (问号) 恰好是该位置中的一个字符。

通配符以外的字符是文本字符。 例如, searchPattern 字符串“*t”搜索以字母“t”结尾的所有名称 path 。 字符串 searchPattern “s*”搜索以字母“s”开头的所有名称 path

注意

在(例如“*.txt”)中使用 searchPattern 星号通配符时,指定扩展名中的字符数会影响搜索,如下所示:

  • 如果指定的扩展名恰好为三个字符,则 该方法返回扩展名以指定扩展名开头的文件。 例如,“*.xls”返回“book.xls”和“book.xlsx”。
  • 在所有其他情况下,方法返回与指定扩展名完全匹配的文件。 例如,“*.ai”返回“file.ai”,但不返回“file.aif”。

使用问号通配符时,此方法仅返回与指定文件扩展名匹配的文件。 例如,给定目录中的两个文件“file1.txt”和“file1.txtother”,“file?.txt”搜索模式仅返回第一个文件,而“file*.txt”搜索模式返回这两个文件。

searchPattern 不能以两个句点结束, (.”) 或包含两个句点 (.”) 后 DirectorySeparatorChar 跟 或 AltDirectorySeparatorChar,也不能包含任何无效字符。 你可以使用 GetInvalidPathChars 方法查询无效字符。

可以使用 参数指定相对路径信息 path 。 相对路径信息解释为相对于当前工作目录的信息,可以使用 方法确定 GetCurrentDirectory 该目录。

EnumerateFileSystemEntriesGetFileSystemEntries 方法的不同之处如下:使用 EnumerateFileSystemEntries时,可以在返回整个集合之前开始枚举条目集合;使用 GetFileSystemEntries时,必须等待整个条目数组返回,然后才能访问数组。 因此,在使用许多文件和目录时, EnumerateFileSystemEntries 可以更高效。

未缓存返回的集合;每次调用 GetEnumerator 集合上的 将启动一个新的枚举。

适用于

EnumerateFileSystemEntries(String, String, EnumerationOptions)

Source:
Directory.cs
Source:
Directory.cs
Source:
Directory.cs

返回指定路径中与搜索模式和枚举选项匹配的文件名和目录名的可枚举集合。

public:
 static System::Collections::Generic::IEnumerable<System::String ^> ^ EnumerateFileSystemEntries(System::String ^ path, System::String ^ searchPattern, System::IO::EnumerationOptions ^ enumerationOptions);
public static System.Collections.Generic.IEnumerable<string> EnumerateFileSystemEntries (string path, string searchPattern, System.IO.EnumerationOptions enumerationOptions);
static member EnumerateFileSystemEntries : string * string * System.IO.EnumerationOptions -> seq<string>
Public Shared Function EnumerateFileSystemEntries (path As String, searchPattern As String, enumerationOptions As EnumerationOptions) As IEnumerable(Of String)

参数

path
String

要搜索的目录的相对或绝对路径。 此字符串不区分大小写。

searchPattern
String

要与 path 中的子目录的名称匹配的搜索字符串。 此参数可以包含有效文本和通配符的组合,但不支持正则表达式。

enumerationOptions
EnumerationOptions

描述要使用的搜索和枚举配置的对象。

返回

一个可枚举集合,它包含 path 指定的目录中与指定搜索模式和指定枚举选项匹配的文件系统项。

例外

.NET Framework 和 2.1 之前的 .NET Core 版本:path是长度为零的字符串,仅包含空格或包含无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

- 或 -

searchPattern 不包含有效模式。

pathnull

searchPattern 上声明的默认值为 null

searchOption 不是有效的 SearchOption 值。

path 无效,如引用未映射的驱动器。

path 是一个文件名。

指定的路径和/或文件名超过了系统定义的最大长度。

调用方没有所要求的权限。

调用方没有所要求的权限。

注解

searchPattern 可以是文本和通配符的组合,但它不支持正则表达式。 允许在 中 searchPattern使用以下通配符说明符。

通配符说明符 匹配
*(星号) 该位置的零个或多个字符。
? (问号) 恰好是该位置中的一个字符。

通配符以外的字符是文本字符。 例如, searchPattern 字符串“*t”搜索以字母“t”结尾的所有名称 path 。 字符串 searchPattern “s*”搜索以字母“s”开头的所有名称 path

注意

在 (如“*.txt”)中使用 searchPattern 星号通配符时,指定扩展名中的字符数会影响搜索,如下所示:

  • 如果指定的扩展名正好为三个字符,该方法将返回扩展名以指定扩展名开头的文件。 例如,“*.xls”同时返回“book.xls”和“book.xlsx”。
  • 在所有其他情况下, 方法返回与指定扩展名完全匹配的文件。 例如,“*.ai”返回“file.ai”,但不返回“file.aif”。

使用问号通配符时,此方法仅返回与指定文件扩展名匹配的文件。 例如,给定目录中的两个文件“file1.txt”和“file1.txtother”,“file?.txt”搜索模式仅返回第一个文件,而“file*.txt”搜索模式返回这两个文件。

searchPattern 不能以两个句点结束, (.”) 或包含两个句点 (“.”) 后 DirectorySeparatorChar 跟 或 AltDirectorySeparatorChar,也不能包含任何无效字符。 你可以使用 GetInvalidPathChars 方法查询无效字符。

可以使用 参数指定相对路径信息 path 。 相对路径信息被解释为相对于当前工作目录的信息,可以使用 方法确定 GetCurrentDirectory 该目录。

EnumerateFileSystemEntriesGetFileSystemEntries 方法的不同之处如下:使用 EnumerateFileSystemEntries时,可以在返回整个集合之前开始枚举条目集合;使用 GetFileSystemEntries时,必须等待返回整个条目数组,然后才能访问数组。 因此,在处理许多文件和目录时, EnumerateFileSystemEntries 可以更高效。

未缓存返回的集合;每次对集合的 调用 GetEnumerator 都将启动一个新的枚举。

适用于

EnumerateFileSystemEntries(String, String, SearchOption)

Source:
Directory.cs
Source:
Directory.cs
Source:
Directory.cs

返回指定路径中与搜索模式匹配的文件名称和目录名的可枚举集合,还可以搜索子目录。

public:
 static System::Collections::Generic::IEnumerable<System::String ^> ^ EnumerateFileSystemEntries(System::String ^ path, System::String ^ searchPattern, System::IO::SearchOption searchOption);
public static System.Collections.Generic.IEnumerable<string> EnumerateFileSystemEntries (string path, string searchPattern, System.IO.SearchOption searchOption);
static member EnumerateFileSystemEntries : string * string * System.IO.SearchOption -> seq<string>
Public Shared Function EnumerateFileSystemEntries (path As String, searchPattern As String, searchOption As SearchOption) As IEnumerable(Of String)

参数

path
String

要搜索的目录的相对或绝对路径。 此字符串不区分大小写。

searchPattern
String

要与 path 中的文件系统项匹配的搜索字符串。 此参数可以包含有效文本路径和通配符(* 和 ?)的组合,但不支持正则表达式。

searchOption
SearchOption

指定搜索操作是应仅包含当前目录还是应包含所有子目录的枚举值之一。 默认值是 TopDirectoryOnly

返回

path 指定的目录中与指定搜索模式和选项匹配的文件系统项的可枚举集合。

例外

.NET Framework和 .NET Core 版本早于 2.1: path 是长度为零的字符串,仅包含空格或包含无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

- 或 -

searchPattern 不包含有效模式。

pathnull

searchPattern 上声明的默认值为 null

searchOption 不是有效的 SearchOption 值。

path 无效,如引用未映射的驱动器。

path 是一个文件名。

指定的路径和/或文件名超过了系统定义的最大长度。

调用方没有所要求的权限。

调用方没有所要求的权限。

注解

searchPattern 可以是文本和通配符的组合,但它不支持正则表达式。 中 searchPattern允许使用以下通配符说明符。

通配符说明符 匹配
*(星号) 该位置的零个或多个字符。
? (问号) 恰好在该位置有一个字符。

通配符以外的字符是文本字符。 例如, searchPattern 字符串“*t”搜索以字母“t”结尾的所有名称 path 。 字符串 searchPattern “s*”搜索以字母“s”开头的所有名称 path

注意

在 (如“*.txt”)中使用 searchPattern 星号通配符时,指定扩展名中的字符数会影响搜索,如下所示:

  • 如果指定的扩展名正好为三个字符,该方法将返回扩展名以指定扩展名开头的文件。 例如,“*.xls”同时返回“book.xls”和“book.xlsx”。
  • 在所有其他情况下, 方法返回与指定扩展名完全匹配的文件。 例如,“*.ai”返回“file.ai”,但不返回“file.aif”。

使用问号通配符时,此方法仅返回与指定文件扩展名匹配的文件。 例如,给定目录中的两个文件“file1.txt”和“file1.txtother”,“file?.txt”搜索模式仅返回第一个文件,而“file*.txt”搜索模式返回这两个文件。

searchPattern 不能以两个句点结束, (.”) 或包含两个句点 (“.”) 后 DirectorySeparatorChar 跟 或 AltDirectorySeparatorChar,也不能包含任何无效字符。 你可以使用 GetInvalidPathChars 方法查询无效字符。

可以使用 参数指定相对路径信息 path 。 相对路径信息被解释为相对于当前工作目录的信息,可以使用 方法确定 GetCurrentDirectory 该目录。

EnumerateFileSystemEntriesGetFileSystemEntries 方法的不同之处如下:使用 EnumerateFileSystemEntries时,可以在返回整个集合之前开始枚举条目集合;使用 GetFileSystemEntries时,必须等待返回整个条目数组,然后才能访问数组。 因此,在处理许多文件和目录时, EnumerateFileSystemEntries 可以更高效。

未缓存返回的集合;每次对集合的 调用 GetEnumerator 都将启动一个新的枚举。

适用于