संपादित करें

इसके माध्यम से साझा किया गया


DirectoryInfo.EnumerateDirectories Method

Definition

Returns an enumerable collection of directory information in the current directory.

Overloads

EnumerateDirectories(String, SearchOption)

Returns an enumerable collection of directory information that matches a specified search pattern and search subdirectory option.

EnumerateDirectories(String, EnumerationOptions)

Returns an enumerable collection of directory information that matches the specified search pattern and enumeration options.

EnumerateDirectories()

Returns an enumerable collection of directory information in the current directory.

EnumerateDirectories(String)

Returns an enumerable collection of directory information that matches a specified search pattern.

EnumerateDirectories(String, SearchOption)

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

Returns an enumerable collection of directory information that matches a specified search pattern and search subdirectory option.

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

Parameters

searchPattern
String

The search string to match against the names of directories. 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. The default value is TopDirectoryOnly.

Returns

An enumerable collection of directories that matches searchPattern and searchOption.

Exceptions

searchPattern is null.

searchOption is not a valid SearchOption value.

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.

Examples

The following example uses this method and the EnumerateFiles method to enumerate the files and directories within the start directory and display the name and size of any files over 10 MB.

using System;
using System.IO;

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

        DirectoryInfo diTop = new DirectoryInfo(docPath);

        try
        {
            foreach (var fi in diTop.EnumerateFiles())
            {
                try
                {
                    // Display each file over 10 MB.
                    if (fi.Length > 10000000)
                    {
                        Console.WriteLine($"{fi.FullName}\t\t{fi.Length:N0}");
                    }
                }
                catch (UnauthorizedAccessException unAuthTop)
                {
                    Console.WriteLine($"{unAuthTop.Message}");
                }
            }

            foreach (var di in diTop.EnumerateDirectories("*"))
            {
                try
                {
                    foreach (var fi in di.EnumerateFiles("*", SearchOption.AllDirectories))
                    {
                        try
                        {
                            // Display each file over 10 MB.
                            if (fi.Length > 10000000)
                            {
                                Console.WriteLine($"{fi.FullName}\t\t{fi.Length:N0}");
                            }
                        }
                        catch (UnauthorizedAccessException unAuthFile)
                        {
                            Console.WriteLine($"unAuthFile: {unAuthFile.Message}");
                        }
                    }
                }
                catch (UnauthorizedAccessException unAuthSubDir)
                {
                    Console.WriteLine($"unAuthSubDir: {unAuthSubDir.Message}");
                }
            }
        }
        catch (DirectoryNotFoundException dirNotFound)
        {
            Console.WriteLine($"{dirNotFound.Message}");
        }
        catch (UnauthorizedAccessException unAuthDir)
        {
            Console.WriteLine($"unAuthDir: {unAuthDir.Message}");
        }
        catch (PathTooLongException longPath)
        {
            Console.WriteLine($"{longPath.Message}");
        }
    }
}
open System
open System.IO

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

let diTop = DirectoryInfo docPath

try
    for fi in diTop.EnumerateFiles() do
        try
            // Display each file over 10 MB.
            if fi.Length > 10000000 then
                printfn $"{fi.FullName}\t\t{fi.Length:N0}"
        with :? UnauthorizedAccessException as unAuthTop ->
            printfn $"{unAuthTop.Message}"

    for di in diTop.EnumerateDirectories "*" do
        try
            for fi in di.EnumerateFiles("*", SearchOption.AllDirectories) do
                try
                    // Display each file over 10 MB.
                    if fi.Length > 10000000 then
                        printfn $"{fi.FullName}\t\t{fi.Length:N0}"
                with :? UnauthorizedAccessException as unAuthFile ->
                    printfn $"unAuthFile: {unAuthFile.Message}"
        with :? UnauthorizedAccessException as unAuthSubDir ->
            printfn $"unAuthSubDir: {unAuthSubDir.Message}"
with
| :? DirectoryNotFoundException as dirNotFound ->
    Console.WriteLine($"{dirNotFound.Message}")
| :? UnauthorizedAccessException as unAuthDir ->
    printfn $"unAuthDir: {unAuthDir.Message}"
| :? PathTooLongException as longPath ->
    printfn $"{longPath.Message}"
Imports System.IO

Class Program
    Public Shared Sub Main(ByVal args As String())
        Dim dirPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        Dim diTop As New DirectoryInfo(dirPath)
        Try
            For Each fi In diTop.EnumerateFiles()
                Try
                    ' Display each file over 10 MB;
                    If fi.Length > 10000000 Then
                        Console.WriteLine("{0}" & vbTab & vbTab & "{1}", fi.FullName, fi.Length.ToString("N0"))
                    End If
                Catch unAuthTop As UnauthorizedAccessException
                    Console.WriteLine($"{unAuthTop.Message}")
                End Try
            Next

            For Each di In diTop.EnumerateDirectories("*")
                Try
                    For Each fi In di.EnumerateFiles("*", SearchOption.AllDirectories)
                        Try
                            ' // Display each file over 10 MB;
                            If fi.Length > 10000000 Then
                                Console.WriteLine("{0}" & vbTab &
                                vbTab & "{1}", fi.FullName, fi.Length.ToString("N0"))
                            End If
                        Catch unAuthFile As UnauthorizedAccessException
                            Console.WriteLine($"unAuthFile: {unAuthFile.Message}")
                        End Try
                    Next
                Catch unAuthSubDir As UnauthorizedAccessException
                    Console.WriteLine($"unAuthSubDir: {unAuthSubDir.Message}")
                End Try
            Next
        Catch dirNotFound As DirectoryNotFoundException
            Console.WriteLine($"{dirNotFound.Message}")
        Catch unAuthDir As UnauthorizedAccessException
            Console.WriteLine($"unAuthDir: {unAuthDir.Message}")
        Catch longPath As PathTooLongException
            Console.WriteLine($"{longPath.Message}")
        End Try
    End Sub
End Class

Remarks

searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. 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. For example, the string "*t" searches for all names in ending with the letter "t". ". The searchPattern string "s*" searches for all names in path beginning with the letter "s".

The EnumerateDirectories and GetDirectories methods differ as follows:

Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

This method pre-populates the values of the following DirectoryInfo properties:

The returned collection is not cached; each call to the GetEnumerator method on the collection will start a new enumeration.

Applies to

EnumerateDirectories(String, EnumerationOptions)

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

Returns an enumerable collection of directory information that matches the specified search pattern and enumeration options.

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

Parameters

searchPattern
String

The search string to match against the names of directories. 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.

Returns

An enumerable collection of directories that matches searchPattern and enumerationOptions.

Exceptions

searchPattern is null.

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.

Remarks

searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. 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. For example, the string "*t" searches for all names in ending with the letter "t". ". The searchPattern string "s*" searches for all names in path beginning with the letter "s".

The EnumerateDirectories and GetDirectories methods differ as follows:

Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

This method pre-populates the values of the following DirectoryInfo properties:

The returned collection is not cached; each call to the GetEnumerator method on the collection will start a new enumeration.

Applies to

EnumerateDirectories()

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

Returns an enumerable collection of directory information in the current directory.

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

Returns

An enumerable collection of directories in the current directory.

Exceptions

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.

Examples

The following example enumerates the subdirectories under the C:\Program Files directory and uses a LINQ query to return the names of all directories that were created before 2009 by checking the value of the CreationTimeUtc property.

If you only need the names of the subdirectories, use the static Directory class for better performance. For an example, see the EnumerateDirectories(String) method.

using System;
using System.IO;

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

            DirectoryInfo dirPrograms = new DirectoryInfo(docPath);
            DateTime StartOf2009 = new DateTime(2009, 01, 01);

            var dirs = from dir in dirPrograms.EnumerateDirectories()
            where dir.CreationTimeUtc > StartOf2009
            select new
            {
                ProgDir = dir,
            };

            foreach (var di in dirs)
            {
                Console.WriteLine($"{di.ProgDir.Name}");
            }
        }
    }
}
// </Snippet1>
module program

// <Snippet1>
open System
open System.IO

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

let dirPrograms = DirectoryInfo docPath
let startOf2009 = DateTime(2009, 01, 01)

let dirs =
    query {
        for dir in dirPrograms.EnumerateDirectories() do
        where (dir.CreationTimeUtc > startOf2009)
        select {| ProgDir = dir |}
    }

for di in dirs do
    printfn $"{di.ProgDir.Name}"
// </Snippet1>
Imports System.IO

Module Module1

    Sub Main()

        Dim dirPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        Dim dirPrograms As New DirectoryInfo(dirPath)
        Dim StartOf2009 As New DateTime(2009, 1, 1)

        Dim dirs = From dir In dirPrograms.EnumerateDirectories()
                   Where dir.CreationTimeUtc > StartOf2009

        For Each di As DirectoryInfo In dirs
            Console.WriteLine("{0}", di.Name)
        Next

    End Sub

End Module

Remarks

The EnumerateDirectories and GetDirectories methods differ as follows:

Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

This method pre-populates the values of the following DirectoryInfo properties:

The returned collection is not cached; each call to the GetEnumerator method on the collection will start a new enumeration.

Applies to

EnumerateDirectories(String)

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

Returns an enumerable collection of directory information that matches a specified search pattern.

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

Parameters

searchPattern
String

The search string to match against the names of directories. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.

Returns

An enumerable collection of directories that matches searchPattern.

Exceptions

searchPattern is null.

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.

Remarks

searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. 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. For example, the string "*t" searches for all names in ending with the letter "t". ". The searchPattern string "s*" searches for all names in path beginning with the letter "s".

The EnumerateDirectories and GetDirectories methods differ as follows:

Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

This method pre-populates the values of the following DirectoryInfo properties:

The returned collection is not cached; each call to the GetEnumerator method on the collection will start a new enumeration.

Applies to