Directory.EnumerateFileSystemEntries 方法

定义

返回满足指定条件的文件系统项的可枚举集合。Returns an enumerable collection of file-system entries that meet specified criteria.

重载

EnumerateFileSystemEntries(String)

返回指定路径中的文件名和目录名的可枚举集合。Returns an enumerable collection of file names and directory names in a specified path.

EnumerateFileSystemEntries(String, String)

返回指定路径中与搜索模式匹配的文件名和目录名的可枚举集合。Returns an enumerable collection of file names and directory names that match a search pattern in a specified path.

EnumerateFileSystemEntries(String, String, EnumerationOptions)

返回指定路径中与搜索模式和枚举选项匹配的文件名和目录名的可枚举集合。Returns an enumerable collection of file names and directory names that match a search pattern and enumeration options in a specified path.

EnumerateFileSystemEntries(String, String, SearchOption)

返回指定路径中与搜索模式匹配的文件名称和目录名的可枚举集合,还可以搜索子目录。Returns an enumerable collection of file names and directory names that match a search pattern in a specified path, and optionally searches subdirectories.

EnumerateFileSystemEntries(String)

返回指定路径中的文件名和目录名的可枚举集合。Returns an enumerable collection of file names and directory names in a specified path.

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

要搜索的目录的相对或绝对路径。The relative or absolute path to the directory to search. 此字符串不区分大小写。This string is not case-sensitive.

返回

IEnumerable<String>

path 指定的目录中的文件系统条目的可枚举集合。An enumerable collection of file-system entries in the directory specified by path.

例外

path 是一个长度为零的字符串,仅包含空格,或包含无效字符。path is a zero-length string, contains only white space, or contains invalid characters. 你可以使用 GetInvalidPathChars() 方法查询无效字符。You can query for invalid characters by using the GetInvalidPathChars() method.

pathnullpath is null.

path 无效,如引用未映射的驱动器。path is invalid, such as referring to an unmapped drive.

path 是一个文件名。path is a file name.

指定的路径和/或文件名超过了系统定义的最大长度。The specified path, file name, or combined exceed the system-defined maximum length.

调用方没有所要求的权限。The caller does not have the required permission.

调用方没有所要求的权限。The caller does not have the required permission.

注解

可以用参数指定相对路径信息 pathYou can specify relative path information with the path parameter. 相对路径信息被解释为相对于当前工作目录,您可以通过使用方法来确定 GetCurrentDirectoryRelative path information is interpreted as relative to the current working directory, which you can determine by using the GetCurrentDirectory method.

EnumerateFileSystemEntriesGetFileSystemEntries 方法的区别如下:使用时 EnumerateFileSystemEntries ,你可以在返回整个集合之前开始枚举条目的集合; 当你使用时 GetFileSystemEntries ,你必须等待返回整个条目数组,然后才能访问数组。The EnumerateFileSystemEntries and GetFileSystemEntries methods differ as follows: When you use EnumerateFileSystemEntries, you can start enumerating the collection of entries before the whole collection is returned; when you use GetFileSystemEntries, you must wait for the whole array of entries 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.

未缓存返回的集合;对集合的每个调用 GetEnumerator 都将开始一个新枚举。The returned collection is not cached; each call to the GetEnumerator on the collection will start a new enumeration.

适用于

EnumerateFileSystemEntries(String, String)

返回指定路径中与搜索模式匹配的文件名和目录名的可枚举集合。Returns an enumerable collection of file names and directory names that match a search pattern in a specified path.

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

要搜索的目录的相对或绝对路径。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 file-system entries in path. 此参数可以包含有效文本路径和通配符(* 和 ?)的组合,但不支持正则表达式。This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.

返回

IEnumerable<String>

path 指定且与指定的搜索模式匹配的目录中的文件系统条目的可枚举集合。An enumerable collection of file-system entries in the directory specified by path and that match the specified search pattern.

例外

path 是一个长度为零的字符串,仅包含空格,或包含无效字符。path is a zero-length string, contains only white space, or contains invalid characters. 你可以使用 GetInvalidPathChars() 方法查询无效字符。You can query for invalid characters by using the GetInvalidPathChars() method.

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

pathnullpath is null.

-or- searchPatternnullsearchPattern is null.

path 无效,如引用未映射的驱动器。path is invalid, such as referring to an unmapped drive.

path 是一个文件名。path is a file name.

指定的路径和/或文件名超过了系统定义的最大长度。The specified path, file name, or combined exceed the system-defined maximum length.

调用方没有所要求的权限。The caller does not have the required permission.

调用方没有所要求的权限。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. 中允许使用以下通配符说明符 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 如 " * .txt")时,指定扩展名中的字符数将影响搜索,如下所示:When you use the asterisk wildcard character in a searchPattern such as "*.txt", the number of characters in the specified extension affects the search as follows:

  • 如果指定的扩展长度恰好为三个字符,则该方法将返回扩展名以指定扩展名开头的文件。If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. 例如," * .xls" 同时返回 "book.xls" 和 "book.xlsx"。For example, "*.xls" returns both "book.xls" and "book.xlsx".
  • 在所有其他情况下,该方法将返回完全与指定扩展名匹配的文件。In all other cases, the method returns files that exactly match the specified extension. 例如," * ai" 返回 "file.ai",而不是 "file. aif"。For example, "*.ai" returns "file.ai" but not "file.aif".

使用问号通配符时,此方法只返回与指定文件扩展名匹配的文件。When you use the question mark wildcard character, this method returns only files that match the specified file extension. 例如,给定两个文件,即 "file1.txt" 和 "file1.txt其他",在目录中,"文件?" 的搜索模式。txt "只返回第一个文件,而" file .txt "的搜索模式 * 返回这两个文件。For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, whereas a search pattern of "file*.txt" returns both files.

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.

可以用参数指定相对路径信息 pathYou can specify relative path information with the path parameter. 相对路径信息被解释为相对于当前工作目录,您可以通过使用方法来确定 GetCurrentDirectoryRelative path information is interpreted as relative to the current working directory, which you can determine by using the GetCurrentDirectory method.

EnumerateFileSystemEntriesGetFileSystemEntries 方法的区别如下:使用时 EnumerateFileSystemEntries ,你可以在返回整个集合之前开始枚举条目的集合; 当你使用时 GetFileSystemEntries ,你必须等待返回整个条目数组,然后才能访问数组。The EnumerateFileSystemEntries and GetFileSystemEntries methods differ as follows: When you use EnumerateFileSystemEntries, you can start enumerating the collection of entries before the whole collection is returned; when you use GetFileSystemEntries, you must wait for the whole array of entries 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.

未缓存返回的集合;对集合的每个调用 GetEnumerator 都将开始一个新枚举。The returned collection is not cached; each call to the GetEnumerator on the collection will start a new enumeration.

适用于

EnumerateFileSystemEntries(String, String, EnumerationOptions)

返回指定路径中与搜索模式和枚举选项匹配的文件名和目录名的可枚举集合。Returns an enumerable collection of file names and directory names that match a search pattern and enumeration options in a specified path.

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

要搜索的目录的相对或绝对路径。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.

返回

IEnumerable<String>

一个可枚举集合,它包含 path 指定的目录中与指定搜索模式和指定枚举选项匹配的文件系统项。An enumerable collection of file-system entries in the directory specified by path, that match the specified search pattern and the specified enumeration options.

例外

path 是一个长度为零的字符串,仅包含空格,或包含无效字符。path is a zero-length string, contains only white space, or contains invalid characters. 你可以使用 GetInvalidPathChars() 方法查询无效字符。You can query for invalid characters by using the GetInvalidPathChars() method.

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

pathnullpath is null.

-or- searchPatternnullsearchPattern is null.

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

path 无效,如引用未映射的驱动器。path is invalid, such as referring to an unmapped drive.

path 是一个文件名。path is a file name.

指定的路径和/或文件名超过了系统定义的最大长度。The specified path, file name, or combined exceed the system-defined maximum length.

调用方没有所要求的权限。The caller does not have the required permission.

调用方没有所要求的权限。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. 中允许使用以下通配符说明符 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 如 " * .txt")时,指定扩展名中的字符数将影响搜索,如下所示:When you use the asterisk wildcard character in a searchPattern such as "*.txt", the number of characters in the specified extension affects the search as follows:

  • 如果指定的扩展长度恰好为三个字符,则该方法将返回扩展名以指定扩展名开头的文件。If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. 例如," * .xls" 同时返回 "book.xls" 和 "book.xlsx"。For example, "*.xls" returns both "book.xls" and "book.xlsx".
  • 在所有其他情况下,该方法将返回完全与指定扩展名匹配的文件。In all other cases, the method returns files that exactly match the specified extension. 例如," * ai" 返回 "file.ai",而不是 "file. aif"。For example, "*.ai" returns "file.ai" but not "file.aif".

使用问号通配符时,此方法只返回与指定文件扩展名匹配的文件。When you use the question mark wildcard character, this method returns only files that match the specified file extension. 例如,给定两个文件,即 "file1.txt" 和 "file1.txt其他",在目录中,"文件?" 的搜索模式。txt "只返回第一个文件,而" file .txt "的搜索模式 * 返回这两个文件。For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, whereas a search pattern of "file*.txt" returns both files.

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.

可以用参数指定相对路径信息 pathYou can specify relative path information with the path parameter. 相对路径信息被解释为相对于当前工作目录,您可以通过使用方法来确定 GetCurrentDirectoryRelative path information is interpreted as relative to the current working directory, which you can determine by using the GetCurrentDirectory method.

EnumerateFileSystemEntriesGetFileSystemEntries 方法的区别如下:使用时 EnumerateFileSystemEntries ,你可以在返回整个集合之前开始枚举条目的集合; 当你使用时 GetFileSystemEntries ,你必须等待返回整个条目数组,然后才能访问数组。The EnumerateFileSystemEntries and GetFileSystemEntries methods differ as follows: When you use EnumerateFileSystemEntries, you can start enumerating the collection of entries before the whole collection is returned; when you use GetFileSystemEntries, you must wait for the whole array of entries 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.

未缓存返回的集合;对集合的每个调用 GetEnumerator 都将开始一个新枚举。The returned collection is not cached; each call to the GetEnumerator on the collection will start a new enumeration.

适用于

EnumerateFileSystemEntries(String, String, SearchOption)

返回指定路径中与搜索模式匹配的文件名称和目录名的可枚举集合,还可以搜索子目录。Returns an enumerable collection of file names and directory names that match a search pattern in a specified path, and optionally searches subdirectories.

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

要搜索的目录的相对或绝对路径。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 file-system entries in path. 此参数可以包含有效文本路径和通配符(* 和 ?)的组合,但不支持正则表达式。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 should include all subdirectories. 默认值是 TopDirectoryOnlyThe default value is TopDirectoryOnly.

返回

IEnumerable<String>

path 指定的目录中与指定搜索模式和选项匹配的文件系统项的可枚举集合。An enumerable collection of file-system entries in the directory specified by path and that match the specified search pattern and option.

例外

path 是一个长度为零的字符串,仅包含空格,或包含无效字符。path is a zero-length string, contains only white space, or contains invalid characters. 你可以使用 GetInvalidPathChars() 方法查询无效字符。You can query for invalid characters by using the GetInvalidPathChars() method.

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

pathnullpath is null.

-or- searchPatternnullsearchPattern is null.

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

path 无效,如引用未映射的驱动器。path is invalid, such as referring to an unmapped drive.

path 是一个文件名。path is a file name.

指定的路径和/或文件名超过了系统定义的最大长度。The specified path, file name, or combined exceed the system-defined maximum length.

调用方没有所要求的权限。The caller does not have the required permission.

调用方没有所要求的权限。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. 中允许使用以下通配符说明符 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 如 " * .txt")时,指定扩展名中的字符数将影响搜索,如下所示:When you use the asterisk wildcard character in a searchPattern such as "*.txt", the number of characters in the specified extension affects the search as follows:

  • 如果指定的扩展长度恰好为三个字符,则该方法将返回扩展名以指定扩展名开头的文件。If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. 例如," * .xls" 同时返回 "book.xls" 和 "book.xlsx"。For example, "*.xls" returns both "book.xls" and "book.xlsx".
  • 在所有其他情况下,该方法将返回完全与指定扩展名匹配的文件。In all other cases, the method returns files that exactly match the specified extension. 例如," * ai" 返回 "file.ai",而不是 "file. aif"。For example, "*.ai" returns "file.ai" but not "file.aif".

使用问号通配符时,此方法只返回与指定文件扩展名匹配的文件。When you use the question mark wildcard character, this method returns only files that match the specified file extension. 例如,给定两个文件,即 "file1.txt" 和 "file1.txt其他",在目录中,"文件?" 的搜索模式。txt "只返回第一个文件,而" file .txt "的搜索模式 * 返回这两个文件。For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, whereas a search pattern of "file*.txt" returns both files.

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.

可以用参数指定相对路径信息 pathYou can specify relative path information with the path parameter. 相对路径信息被解释为相对于当前工作目录,您可以通过使用方法来确定 GetCurrentDirectoryRelative path information is interpreted as relative to the current working directory, which you can determine by using the GetCurrentDirectory method.

EnumerateFileSystemEntriesGetFileSystemEntries 方法的区别如下:使用时 EnumerateFileSystemEntries ,你可以在返回整个集合之前开始枚举条目的集合; 当你使用时 GetFileSystemEntries ,你必须等待返回整个条目数组,然后才能访问数组。The EnumerateFileSystemEntries and GetFileSystemEntries methods differ as follows: When you use EnumerateFileSystemEntries, you can start enumerating the collection of entries before the whole collection is returned; when you use GetFileSystemEntries, you must wait for the whole array of entries 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.

未缓存返回的集合;对集合的每个调用 GetEnumerator 都将开始一个新枚举。The returned collection is not cached; each call to the GetEnumerator on the collection will start a new enumeration.

适用于