Επεξεργασία

Κοινή χρήση μέσω


DirectoryInfo.GetFiles Method

Definition

Returns a file list from the current directory.

Overloads

GetFiles(String, EnumerationOptions)

Returns a file list from the current directory matching the specified search pattern and enumeration options.

GetFiles(String, SearchOption)

Returns a file list from the current directory matching the given search pattern and using a value to determine whether to search subdirectories.

GetFiles()

Returns a file list from the current directory.

GetFiles(String)

Returns a file list from the current directory matching the given search pattern.

GetFiles(String, EnumerationOptions)

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

Returns a file list from the current directory matching the specified search pattern and enumeration options.

public:
 cli::array <System::IO::FileInfo ^> ^ GetFiles(System::String ^ searchPattern, System::IO::EnumerationOptions ^ enumerationOptions);
public System.IO.FileInfo[] GetFiles (string searchPattern, System.IO.EnumerationOptions enumerationOptions);
member this.GetFiles : string * System.IO.EnumerationOptions -> System.IO.FileInfo[]
Public Function GetFiles (searchPattern As String, enumerationOptions As EnumerationOptions) As 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 array of strongly typed FileInfo objects that match searchPattern and enumerationOptions.

Exceptions

.NET Framework and .NET Core versions older than 2.1: searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPattern is null.

The path 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.

If there are no files in the DirectoryInfo, this method returns an empty array.

The following wildcard specifiers are permitted in the searchPattern parameter.

Wildcard character Description
* Zero or more characters.
? Exactly zero or one character.

The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

Wildcards are permitted. For example, the searchPattern string "*.txt" searches for all file names having an extension of "txt". The searchPattern string "s*" searches for all file names beginning with the letter "s". If there are no files, or no files that match the searchPattern string in the DirectoryInfo, this method returns an empty array.

Note

When using the asterisk wildcard character in a searchPattern (for example, "*.txt"), the matching behavior varies depending on the length of the specified file extension. A searchPattern with a file extension of exactly three characters returns files with an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. A searchPattern with a file extension of one, two, or more than three characters returns only files with extensions of exactly that length that match the file extension specified in the searchPattern. When using the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files in a directory, "file1.txt" and "file1.txtother", a search pattern of "file?.txt" returns only the first file, while a search pattern of "file*.txt" returns both files.

Note

Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" will return "longfilename.txt" because the equivalent 8.3 file name format would be "longf~1.txt".

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

Applies to

GetFiles(String, SearchOption)

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

Returns a file list from the current directory matching the given search pattern and using a value to determine whether to search subdirectories.

public:
 cli::array <System::IO::FileInfo ^> ^ GetFiles(System::String ^ searchPattern, System::IO::SearchOption searchOption);
public System.IO.FileInfo[] GetFiles (string searchPattern, System.IO.SearchOption searchOption);
member this.GetFiles : string * System.IO.SearchOption -> System.IO.FileInfo[]
Public Function GetFiles (searchPattern As String, searchOption As SearchOption) As 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.

Returns

An array of type FileInfo.

Exceptions

.NET Framework and .NET Core versions older than 2.1: searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPattern is null.

searchOption is not a valid SearchOption value.

The path 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 get a list of files from 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:\Users\tomfitz\Documents\ExampleDir");
            Console.WriteLine("No search pattern returns:");
            foreach (var fi in di.GetFiles())
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern *2* returns:");
            foreach (var fi in di.GetFiles("*2*"))
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern test?.txt returns:");
            foreach (var fi in di.GetFiles("test?.txt"))
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern AllDirectories returns:");
            foreach (var fi in di.GetFiles("*", 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:\Users\tomfitz\Documents\ExampleDir"
printfn "No search pattern returns:"
for fi in di.GetFiles() do
    printfn $"{fi.Name}"

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

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

printfn "\nSearch pattern AllDirectories returns:"
for fi in di.GetFiles("*", 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.GetFiles()
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern *2* returns:")
        For Each fi In di.GetFiles("*2*")
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern test?.txt returns:")
        For Each fi In di.GetFiles("test?.txt")
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern AllDirectories returns:")
        For Each fi In di.GetFiles("*", 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.

If there are no files in the DirectoryInfo, this method returns an empty array.

The following wildcard specifiers are permitted in searchPattern.

Wildcard character Description
* (asterisk) Zero or more characters.
? (question mark) Exactly zero or one character.

The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

Wildcards are permitted. For example, the searchPattern string "*.txt" searches for all file names having an extension of "txt". The searchPattern string "s*" searches for all file names beginning with the letter "s". If there are no files, or no files that match the searchPattern string in the DirectoryInfo, this method returns an empty array.

Note

When using the asterisk wildcard character in a searchPattern (for example, "*.txt"), the matching behavior varies depending on the length of the specified file extension. A searchPattern with a file extension of exactly three characters returns files with an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. A searchPattern with a file extension of one, two, or more than three characters returns only files with extensions of exactly that length that match the file extension specified in the searchPattern. When using the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files in a directory, "file1.txt" and "file1.txtother", a search pattern of "file?.txt" returns only the first file, while a search pattern of "file*.txt" returns both files.

The following list shows the behavior of different lengths for the searchPattern parameter:

  • "*.abc" returns files having an extension of .abc, .abcd, .abcde, .abcdef, and so on.

  • "*.abcd" returns only files having an extension of .abcd.

  • "*.abcde" returns only files having an extension of .abcde.

  • "*.abcdef" returns only files having an extension of .abcdef.

Note

Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" will return "longfilename.txt" because the equivalent 8.3 file name format would be "longf~1.txt".

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

  1. Attributes

  2. CreationTime

  3. CreationTimeUtc

  4. LastAccessTime

  5. LastAccessTimeUtc

  6. LastWriteTime

  7. LastWriteTimeUtc

  8. Length

See also

Applies to

GetFiles()

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

Returns a file list from the current directory.

public:
 cli::array <System::IO::FileInfo ^> ^ GetFiles();
public System.IO.FileInfo[] GetFiles ();
member this.GetFiles : unit -> System.IO.FileInfo[]
Public Function GetFiles () As FileInfo()

Returns

An array of type FileInfo.

Exceptions

The path is invalid, such as being on an unmapped drive.

Examples

The following example shows how to get a list of files from 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:\Users\tomfitz\Documents\ExampleDir");
            Console.WriteLine("No search pattern returns:");
            foreach (var fi in di.GetFiles())
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern *2* returns:");
            foreach (var fi in di.GetFiles("*2*"))
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern test?.txt returns:");
            foreach (var fi in di.GetFiles("test?.txt"))
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern AllDirectories returns:");
            foreach (var fi in di.GetFiles("*", 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:\Users\tomfitz\Documents\ExampleDir"
printfn "No search pattern returns:"
for fi in di.GetFiles() do
    printfn $"{fi.Name}"

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

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

printfn "\nSearch pattern AllDirectories returns:"
for fi in di.GetFiles("*", 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.GetFiles()
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern *2* returns:")
        For Each fi In di.GetFiles("*2*")
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern test?.txt returns:")
        For Each fi In di.GetFiles("test?.txt")
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern AllDirectories returns:")
        For Each fi In di.GetFiles("*", 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.

If there are no files in the DirectoryInfo, this method returns an empty array.

The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

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

See also

Applies to

GetFiles(String)

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

Returns a file list from the current directory matching the given search pattern.

public:
 cli::array <System::IO::FileInfo ^> ^ GetFiles(System::String ^ searchPattern);
public System.IO.FileInfo[] GetFiles (string searchPattern);
member this.GetFiles : string -> System.IO.FileInfo[]
Public Function GetFiles (searchPattern As String) As 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 array of type FileInfo.

Exceptions

.NET Framework and .NET Core versions older than 2.1: searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPattern is null.

The path 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 get a list of files from 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:\Users\tomfitz\Documents\ExampleDir");
            Console.WriteLine("No search pattern returns:");
            foreach (var fi in di.GetFiles())
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern *2* returns:");
            foreach (var fi in di.GetFiles("*2*"))
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern test?.txt returns:");
            foreach (var fi in di.GetFiles("test?.txt"))
            {
                Console.WriteLine(fi.Name);
            }

            Console.WriteLine();

            Console.WriteLine("Search pattern AllDirectories returns:");
            foreach (var fi in di.GetFiles("*", 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:\Users\tomfitz\Documents\ExampleDir"
printfn "No search pattern returns:"
for fi in di.GetFiles() do
    printfn $"{fi.Name}"

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

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

printfn "\nSearch pattern AllDirectories returns:"
for fi in di.GetFiles("*", 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.GetFiles()
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern *2* returns:")
        For Each fi In di.GetFiles("*2*")
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern test?.txt returns:")
        For Each fi In di.GetFiles("test?.txt")
            Console.WriteLine(fi.Name)
        Next

        Console.WriteLine()

        Console.WriteLine("Search pattern AllDirectories returns:")
        For Each fi In di.GetFiles("*", 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.

If there are no files in the DirectoryInfo, this method returns an empty array.

The following wildcard specifiers are permitted in the searchPattern parameter.

Wildcard character Description
* Zero or more characters.
? Exactly zero or one character.

The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

Wildcards are permitted. For example, the searchPattern string "*.txt" searches for all file names having an extension of "txt". The searchPattern string "s*" searches for all file names beginning with the letter "s". If there are no files, or no files that match the searchPattern string in the DirectoryInfo, this method returns an empty array.

Note

When using the asterisk wildcard character in a searchPattern (for example, "*.txt"), the matching behavior varies depending on the length of the specified file extension. A searchPattern with a file extension of exactly three characters returns files with an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. A searchPattern with a file extension of one, two, or more than three characters returns only files with extensions of exactly that length that match the file extension specified in the searchPattern. When using the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files in a directory, "file1.txt" and "file1.txtother", a search pattern of "file?.txt" returns only the first file, while a search pattern of "file*.txt" returns both files.

Note

Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" will return "longfilename.txt" because the equivalent 8.3 file name format would be "longf~1.txt".

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

See also

Applies to