Directory.EnumerateDirectories 方法

定义

返回满足指定条件的目录完整名称的可枚举集合。

重载

EnumerateDirectories(String)

返回指定路径中的目录完整名称的可枚举集合。

EnumerateDirectories(String, String)

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

EnumerateDirectories(String, String, EnumerationOptions)

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

EnumerateDirectories(String, String, SearchOption)

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

EnumerateDirectories(String)

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

返回指定路径中的目录完整名称的可枚举集合。

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

参数

path
String

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

返回

一个可枚举集合,它包含目录中由 path 指定的目录的完整名称(包括路径)。

例外

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

pathnull

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

path 是一个文件名。

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

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

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

示例

以下示例枚举指定路径中的顶级目录。

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    private static void Main(string[] args)
    {
        try
        {
            // Set a variable to the My Documents path.
            string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            List<string> dirs = new List<string>(Directory.EnumerateDirectories(docPath));

            foreach (var dir in dirs)
            {
                Console.WriteLine($"{dir.Substring(dir.LastIndexOf(Path.DirectorySeparatorChar) + 1)}");
            }
            Console.WriteLine($"{dirs.Count} directories found.");
        }
        catch (UnauthorizedAccessException ex)
        {
            Console.WriteLine(ex.Message);
        }
        catch (PathTooLongException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}
open System
open System.IO

try
    // Set a variable to the My Documents path.
    let docPath = Environment.GetFolderPath Environment.SpecialFolder.MyDocuments

    let dirs = Directory.EnumerateDirectories docPath |> Seq.toList

    for dir in dirs do
        printfn $"{dir.Substring(dir.LastIndexOf Path.DirectorySeparatorChar + 1)}"
    printfn $"{dirs.Length} directories found."

with
| :? UnauthorizedAccessException as ex ->
    printfn $"{ex.Message}"
| :? PathTooLongException as ex ->
    printfn $"{ex.Message}"
Imports System.Collections.Generic
Imports System.IO

Module Module1

    Sub Main()
        Try
            Dim dirPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

            Dim dirs As List(Of String) = New List(Of String)(Directory.EnumerateDirectories(dirPath))

            For Each folder In dirs
                Console.WriteLine($"{dir.Substring(dir.LastIndexOf(Path.DirectorySeparatorChar) + 1)}")
            Next
            Console.WriteLine($"{dirs.Count} directories found.")
        Catch ex As UnauthorizedAccessException
            Console.WriteLine(ex.Message)
        Catch ex As PathTooLongException
            Console.WriteLine(ex.Message)
        End Try

    End Sub
End Module

注解

可以在 参数中 path 指定相对或绝对路径信息。 相对路径信息被解释为相对于当前工作目录的信息,可以使用 方法确定 GetCurrentDirectory 该目录。 返回的目录名称以参数中 path 提供的值作为前缀。 例如,如果在 参数中 path 提供相对路径,则返回的目录名称将包含相对路径。

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

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

适用于

EnumerateDirectories(String, String)

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

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

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

参数

path
String

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

searchPattern
String

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

返回

path 指定且与指定的搜索模式相匹配的目录中的文件的全名(包括路径)的可枚举集合。

例外

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

- 或 -

searchPattern 不包含有效模式。

pathnull

searchPatternnull

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

path 是一个文件名。

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

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

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

示例

以下示例枚举指定路径中与指定搜索模式匹配的顶级目录。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Program
{

    private static void Main(string[] args)
    {
        try
        {
            string dirPath = @"\\archives\2009\reports";

            // LINQ query.
            var dirs = from dir in
                     Directory.EnumerateDirectories(dirPath, "dv_*")
                       select dir;

            // Show results.
            foreach (var dir in dirs)
            {
                // Remove path information from string.
                Console.WriteLine("{0}",
                    dir.Substring(dir.LastIndexOf("\\") + 1));
            }
            Console.WriteLine("{0} directories found.",
                dirs.Count<string>().ToString());

            // Optionally create a List collection.
            List<string> workDirs = new List<string>(dirs);
        }
        catch (UnauthorizedAccessException UAEx)
        {
            Console.WriteLine(UAEx.Message);
        }
        catch (PathTooLongException PathEx)
        {
            Console.WriteLine(PathEx.Message);
        }
    }
}
open System
open System.IO

try
    let dirPath = @"\\archives\2009\reports"

    let dirs = 
        Directory.EnumerateDirectories(dirPath, "dv_*")
        |> Seq.cache

    // Show results.
    for dir in dirs do
        // Remove path information from string.
        printfn $"{dir.Substring(dir.LastIndexOf '\\' + 1)}"
    printfn $"{Seq.length dirs} directories found."

    // Optionally create a list collection.
    let workDirs = Seq.toList dirs
    ()
    
with 
| :? UnauthorizedAccessException as uaEx ->
    printfn $"{uaEx.Message}"
| :? PathTooLongException as pathEx ->
    printfn $"{pathEx.Message}"
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq

Module Module1

    Sub Main()
        Try
            Dim dirPath As String = "\\archives\2009\reports"

            ' LINQ query.
            Dim dirs = From folder In _
                Directory.EnumerateDirectories(dirPath, "dv_*")
            For Each folder In dirs
                ' Remove path infomration from string.
                Console.WriteLine("{0}", _
                        folder.Substring(folder.LastIndexOf("\") + 1))
            Next
            Console.WriteLine("{0} directories found.", _
                dirs.Count.ToString())

            ' Optionally create a List collection.
            Dim workDirs As List(Of String) = New List(Of String)

        Catch UAEx As UnauthorizedAccessException
            Console.WriteLine(UAEx.Message)
        Catch PathEx As PathTooLongException
            Console.WriteLine(PathEx.Message)
        End Try
    End Sub
End Module

注解

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

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

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

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

可以在 参数中 path 指定相对或绝对路径信息。 相对路径信息被解释为相对于当前工作目录的信息,可以使用 方法确定 GetCurrentDirectory 该目录。 返回的目录名称以参数中 path 提供的值作为前缀。 例如,如果在 参数中 path 提供相对路径,则返回的目录名称将包含相对路径。

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

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

适用于

EnumerateDirectories(String, String, EnumerationOptions)

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

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

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

参数

path
String

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

searchPattern
String

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

enumerationOptions
EnumerationOptions

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

返回

一个可枚举集合,它包含 path 指定的目录中与指定的搜索模式和枚举选项匹配的目录的完整名称(包括路径)。

例外

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

- 或 -

searchPattern 不包含有效模式。

pathsearchPatternnull

searchOption 不是有效的 SearchOption 值。

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

path 是一个文件名。

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

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

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

注解

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

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

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

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

可以在 参数中 path 指定相对或绝对路径信息。 相对路径信息被解释为相对于当前工作目录的信息,可以使用 方法确定 GetCurrentDirectory 该目录。 返回的目录名称以参数中 path 提供的值作为前缀。 例如,如果在 参数中 path 提供相对路径,则返回的目录名称将包含相对路径。

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

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

适用于

EnumerateDirectories(String, String, SearchOption)

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

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

public:
 static System::Collections::Generic::IEnumerable<System::String ^> ^ EnumerateDirectories(System::String ^ path, System::String ^ searchPattern, System::IO::SearchOption searchOption);
public static System.Collections.Generic.IEnumerable<string> EnumerateDirectories (string path, string searchPattern, System.IO.SearchOption searchOption);
static member EnumerateDirectories : string * string * System.IO.SearchOption -> seq<string>
Public Shared Function EnumerateDirectories (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 是一个文件名。

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

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

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

示例

以下示例枚举与指定搜索模式匹配的指定路径中的目录。 它使用 searchOption 参数指定搜索中应包括所有子目录。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Program
{

    private static void Main(string[] args)
    {
        try
        {
            string dirPath = @"\\archives\2009\reports";

            // LINQ query.
            var dirs = from dir in
                     Directory.EnumerateDirectories(dirPath, "dv_*",
                        SearchOption.AllDirectories)
                       select dir;

            // Show results.
            foreach (var dir in dirs)
            {
                // Remove path information from string.
                Console.WriteLine("{0}",
                    dir.Substring(dir.LastIndexOf("\\") + 1));
            }
            Console.WriteLine("{0} directories found.",
                dirs.Count<string>().ToString());

            // Optionally create a List collection.
            List<string> workDirs = new List<string>(dirs);
        }
        catch (UnauthorizedAccessException UAEx)
        {
            Console.WriteLine(UAEx.Message);
        }
        catch (PathTooLongException PathEx)
        {
            Console.WriteLine(PathEx.Message);
        }
    }
}
open System
open System.IO

try
    let dirPath = @"\\archives\2009\reports"

    let dirs =
        Directory.EnumerateDirectories(dirPath, "dv_*", SearchOption.AllDirectories)
        |> Seq.cache

    // Show results.
    for dir in dirs do
        // Remove path information from string.
        printfn $"{dir.Substring(dir.LastIndexOf '\\' + 1)}"
    printfn $"{Seq.length dirs} directories found."

    // Optionally create a List collection.
    let workDirs = Seq.toList dirs
    ()

with 
| :? UnauthorizedAccessException as uaEx ->
    printfn $"{uaEx.Message}"
| :? PathTooLongException as pathEx ->
    printfn $"{pathEx.Message}"
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq

Module Module1

    Sub Main()
        Try
            Dim dirPath As String = "\\archives\2009\reports"

            ' LINQ query.
            Dim dirs = From folder In _
                Directory.EnumerateDirectories(dirPath, "dv_*", _
                    SearchOption.AllDirectories)
            For Each folder In dirs
                ' Remove path infomration from string.
                Console.WriteLine("{0}", _
                        folder.Substring(folder.LastIndexOf("\") + 1))
            Next
            Console.WriteLine("{0} directories found.", _
                dirs.Count.ToString())

            ' Optionally create a List collection.
            Dim workDirs As List(Of String) = New List(Of String)

        Catch UAEx As UnauthorizedAccessException
            Console.WriteLine(UAEx.Message)
        Catch PathEx As PathTooLongException
            Console.WriteLine(PathEx.Message)
        End Try
    End Sub
End Module

注解

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

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

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

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

可以在 参数中 path 指定相对或绝对路径信息。 相对路径信息被解释为相对于当前工作目录的信息,可以使用 方法确定 GetCurrentDirectory 该目录。 返回的目录名称以参数中 path 提供的值作为前缀。 例如,如果在 参数中 path 提供相对路径,则返回的目录名称将包含相对路径。

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

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

适用于