Uredi

Deli z drugimi prek


DirectoryInfo.EnumerateFiles Method

Definition

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

Overloads

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()

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

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)

Returns

An enumerable collection of the files 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 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.

If you only need the names of the files, use the static Directory class for better performance. 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.
let dirInfo = DirectoryInfo @"\archives1\library\"

let startOf2009 = DateTime(2009, 01, 01)

// LINQ query for all files created before 2009.
let files = 
    query {
        for f in dirInfo.EnumerateFiles() do
        where (f.CreationTime < startOf2009)
        select f
    }

// Show results.
for f in files do
    printfn $"{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. 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 . . .

*/
open System.IO

let di = DirectoryInfo @"C:\ExampleDir"
printfn "No search pattern returns:"
for fi in di.EnumerateFiles() do
    printfn $"{fi.Name}"

printfn "\nSearch pattern *2* returns:"
for fi in di.EnumerateFiles "*2*" do
    printfn $"{fi.Name}"

printfn "\nSearch pattern test?.txt returns:"
for fi in di.EnumerateFiles "test?.txt" do
    printfn $"{fi.Name}"

printfn "\nSearch pattern AllDirectories returns:"
for fi in di.EnumerateFiles("*", SearchOption.AllDirectories) do
    printfn $"{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 . . .

Remarks

The EnumerateFiles and GetFiles methods differ as follows:

  • When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

  • When you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array.

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

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

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

Applies to

EnumerateFiles(String)

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

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)

Parameters

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.

Returns

An enumerable collection of files 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.

Examples

The following example shows how to enumerate files in a directory by using different search options. 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 . . .

*/
open System.IO

let di = DirectoryInfo @"C:\ExampleDir"
printfn "No search pattern returns:"
for fi in di.EnumerateFiles() do
    printfn $"{fi.Name}"

printfn "\nSearch pattern *2* returns:"
for fi in di.EnumerateFiles "*2*" do
    printfn $"{fi.Name}"

printfn "\nSearch pattern test?.txt returns:"
for fi in di.EnumerateFiles "test?.txt" do
    printfn $"{fi.Name}"

printfn "\nSearch pattern AllDirectories returns:"
for fi in di.EnumerateFiles("*", SearchOption.AllDirectories) do
    printfn $"{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 . . .

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 EnumerateFiles and GetFiles methods differ as follows:

  • When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

  • When you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array.

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

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

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

Applies to

EnumerateFiles(String, EnumerationOptions)

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

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>
Public Function EnumerateFiles (searchPattern As String, enumerationOptions As EnumerationOptions) As IEnumerable(Of FileInfo)

Parameters

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.

Returns

An enumerable collection of files 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 EnumerateFiles and GetFiles methods differ as follows:

  • When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

  • When you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array.

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

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

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

Applies to

EnumerateFiles(String, SearchOption)

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

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>
Public Function EnumerateFiles (searchPattern As String, searchOption As SearchOption) As IEnumerable(Of FileInfo)

Parameters

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. The default value is TopDirectoryOnly.

Returns

An enumerable collection of files 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 shows how to enumerate files in a directory by using different search options. 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 . . .

*/
open System.IO

let di = DirectoryInfo @"C:\ExampleDir"
printfn "No search pattern returns:"
for fi in di.EnumerateFiles() do
    printfn $"{fi.Name}"

printfn "\nSearch pattern *2* returns:"
for fi in di.EnumerateFiles "*2*" do
    printfn $"{fi.Name}"

printfn "\nSearch pattern test?.txt returns:"
for fi in di.EnumerateFiles "test?.txt" do
    printfn $"{fi.Name}"

printfn "\nSearch pattern AllDirectories returns:"
for fi in di.EnumerateFiles("*", SearchOption.AllDirectories) do
    printfn $"{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 . . .

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 EnumerateFiles and GetFiles methods differ as follows:

  • When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

  • When you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array.

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

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

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

Applies to