DirectoryInfo.GetFiles 方法

定义

返回当前目录的文件列表。Returns a file list from the current directory.

重载

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)

返回当前目录中与指定的搜索模式和枚举选项匹配的文件列表。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[]

参数

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.

返回

FileInfosearchPattern 匹配的强类型化 enumerationOptions 对象的数组。An array of strongly typed FileInfo objects that match searchPattern and enumerationOptions.

异常

searchPattern 包含一个或多个 GetInvalidPathChars() 方法定义的无效字符。searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPatternnullsearchPattern is null.

路径无效(例如,它位于未映射的驱动器上)。The path is invalid (for example, it is on an unmapped drive).

调用方没有所要求的权限。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. searchPattern中允许使用以下通配符说明符。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. 例如,字符串 "*t" 搜索以字母 "t" 结尾的所有名称。For example, the string "*t" searches for all names in ending with the letter "t". searchPattern 字符串 "s*" 搜索 path 以字母 "s" 开头的所有名称。The searchPattern string "s*" searches for all names in path beginning with the letter "s".

EnumerateFilesGetFiles 方法的区别如下:The EnumerateFiles and GetFiles methods differ as follows:

  • 使用 EnumerateFiles时,可以在返回整个集合之前,开始枚举 FileInfo 对象的集合。When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

  • 使用 GetFiles时,必须等待返回整个 FileInfo 对象数组,然后才能访问数组。When you use GetFiles, you must wait for the whole array of FileInfo objects 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.

如果 DirectoryInfo中没有文件,则此方法返回一个空数组。If there are no files in the DirectoryInfo, this method returns an empty array.

searchPattern 参数中允许使用以下通配符说明符。The following wildcard specifiers are permitted in the searchPattern parameter.

通配符Wildcard character 说明Description
* 零个或多个字符。Zero or more characters.
?? 正好为零或一个字符。Exactly zero or one character.

不保证返回的文件名的顺序;如果需要特定的排序顺序,请使用 Sort 方法。The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

允许使用通配符。Wildcards are permitted. 例如,searchPattern 字符串 "* .txt" 搜索扩展名为 "txt" 的所有文件名。For example, the searchPattern string "*.txt" searches for all file names having an extension of "txt". searchPattern 字符串 "s*" 搜索所有以字母 "s" 开头的文件名称。The searchPattern string "s*" searches for all file names beginning with the letter "s". 如果没有文件,或者没有与 DirectoryInfo中的 searchPattern 字符串匹配的文件,则此方法将返回一个空数组。If there are no files, or no files that match the searchPattern string in the DirectoryInfo, this method returns an empty array.

备注

searchPattern 中使用星号通配符(例如 "* .txt")时,匹配行为因指定文件扩展名的长度而异。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. 文件扩展名正好为三个字符的 searchPattern 返回扩展名为三个或更多字符的文件,其中前三个字符与 searchPattern中指定的文件扩展名相匹配。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. 文件扩展名为一个、两个或三个以上的字符的 searchPattern 仅返回扩展名正好与 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. 例如,如果一个目录中有两个文件,"file1" 和 "txtother",则为 "file?" 的搜索模式。txt "仅返回第一个文件,而" file*.txt "的搜索模式返回这两个文件。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.

备注

由于此方法使用8.3 文件名格式和长文件名格式的文件名进行检查,因此类似于 "* 1*.txt" 的搜索模式可能会返回意外的文件名。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. 例如,使用搜索模式 "*1*.txt" 将返回 "longfilename",因为等效的8.3 文件名格式为 "longf ~ 1 .txt"。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".

此方法预先填充以下 FileInfo 属性的值:This method pre-populates the values of the following FileInfo properties:

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.

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[]

参数

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.

返回

一个类型为 FileInfo 的数组。An array of type FileInfo.

异常

searchPattern 包含一个或多个 GetInvalidPathChars() 方法定义的无效字符。searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPatternnullsearchPattern is null.

searchOption 不是有效的 SearchOption 值。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.

示例

下面的示例演示如何使用不同的搜索选项从目录中获取文件列表。The following example shows how to get a list of files from a directory by using different search options. 该示例假设有一个目录,其中包含名为 log1.txt、log2、test1、test2、test3 和包含名为子文件的文件的子目录。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 . . .

*/
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 . . .

注解

EnumerateFilesGetFiles 方法的区别如下:The EnumerateFiles and GetFiles methods differ as follows:

  • 使用 EnumerateFiles时,可以在返回整个集合之前,开始枚举 FileInfo 对象的集合。When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

  • 使用 GetFiles时,必须等待返回整个 FileInfo 对象数组,然后才能访问数组。When you use GetFiles, you must wait for the whole array of FileInfo objects 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.

如果 DirectoryInfo中没有文件,则此方法返回一个空数组。If there are no files in the DirectoryInfo, this method returns an empty array.

searchPattern中允许使用以下通配符说明符。The following wildcard specifiers are permitted in searchPattern.

通配符Wildcard character 说明Description
* (星号)* (asterisk) 零个或多个字符。Zero or more characters.
?? (问号)(question mark) 正好为零或一个字符。Exactly zero or one character.

不保证返回的文件名的顺序;如果需要特定的排序顺序,请使用 Sort 方法。The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

允许使用通配符。Wildcards are permitted. 例如,searchPattern 字符串 "* .txt" 搜索扩展名为 "txt" 的所有文件名。For example, the searchPattern string "*.txt" searches for all file names having an extension of "txt". searchPattern 字符串 "s*" 搜索所有以字母 "s" 开头的文件名称。The searchPattern string "s*" searches for all file names beginning with the letter "s". 如果没有文件,或者没有与 DirectoryInfo中的 searchPattern 字符串匹配的文件,则此方法将返回一个空数组。If there are no files, or no files that match the searchPattern string in the DirectoryInfo, this method returns an empty array.

备注

searchPattern 中使用星号通配符(例如 "* .txt")时,匹配行为因指定文件扩展名的长度而异。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. 文件扩展名正好为三个字符的 searchPattern 返回扩展名为三个或更多字符的文件,其中前三个字符与 searchPattern中指定的文件扩展名相匹配。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. 文件扩展名为一个、两个或三个以上的字符的 searchPattern 仅返回扩展名正好与 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. 例如,如果一个目录中有两个文件,"file1" 和 "txtother",则为 "file?" 的搜索模式。txt "仅返回第一个文件,而" file*.txt "的搜索模式返回这两个文件。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.

以下列表显示 searchPattern 参数的不同长度的行为:The following list shows the behavior of different lengths for the searchPattern parameter:

  • "* abc" 返回扩展名为 abc、. abcd、. abcde...z、. abcdef 的文件,依此类推。"*.abc" returns files having an extension of.abc,.abcd,.abcde,.abcdef, and so on.

  • "* abcd" 仅返回扩展名为 abcd 的文件。"*.abcd" returns only files having an extension of.abcd.

  • "* abcde...z" 只返回扩展名为 abcde...z 的文件。"*.abcde" returns only files having an extension of.abcde.

  • "* abcdef" 只返回扩展名为 abcdef 的文件。"*.abcdef" returns only files having an extension of.abcdef.

备注

由于此方法使用8.3 文件名格式和长文件名格式的文件名进行检查,因此类似于 "* 1*.txt" 的搜索模式可能会返回意外的文件名。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. 例如,使用搜索模式 "*1*.txt" 将返回 "longfilename",因为等效的8.3 文件名格式为 "longf ~ 1 .txt"。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".

此方法预先填充以下 FileInfo 属性的值: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

另请参阅

GetFiles()

返回当前目录的文件列表。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()

返回

一个类型为 FileInfo 的数组。An array of type FileInfo.

异常

路径无效(例如,路径位于未映射的驱动器上)。The path is invalid, such as being on an unmapped drive.

示例

下面的示例演示如何使用不同的搜索选项从目录中获取文件列表。The following example shows how to get a list of files from a directory by using different search options. 该示例假设有一个目录,其中包含名为 log1.txt、log2、test1、test2、test3 和包含名为子文件的文件的子目录。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 . . .

*/
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 . . .

注解

EnumerateFilesGetFiles 方法的区别如下:The EnumerateFiles and GetFiles methods differ as follows:

  • 使用 EnumerateFiles时,可以在返回整个集合之前,开始枚举 FileInfo 对象的集合。When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

  • 使用 GetFiles时,必须等待返回整个 FileInfo 对象数组,然后才能访问数组。When you use GetFiles, you must wait for the whole array of FileInfo objects 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.

如果 DirectoryInfo中没有文件,则此方法返回一个空数组。If there are no files in the DirectoryInfo, this method returns an empty array.

不保证返回的文件名的顺序;如果需要特定的排序顺序,请使用 Sort 方法。The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

此方法预先填充以下 FileInfo 属性的值:This method pre-populates the values of the following FileInfo properties:

另请参阅

GetFiles(String)

返回当前目录中与给定的搜索模式匹配的文件列表。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()

参数

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.

返回

一个类型为 FileInfo 的数组。An array of type FileInfo.

异常

searchPattern 包含一个或多个 GetInvalidPathChars() 方法定义的无效字符。searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPatternnullsearchPattern is null.

路径无效(例如,它位于未映射的驱动器上)。The path is invalid (for example, it is on an unmapped drive).

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

示例

下面的示例演示如何使用不同的搜索选项从目录中获取文件列表。The following example shows how to get a list of files from a directory by using different search options. 该示例假设有一个目录,其中包含名为 log1.txt、log2、test1、test2、test3 和包含名为子文件的文件的子目录。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 . . .

*/
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 . . .

注解

searchPattern 可以是文字和通配符的组合,但不支持正则表达式。searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. searchPattern中允许使用以下通配符说明符。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. 例如,字符串 "*t" 搜索以字母 "t" 结尾的所有名称。For example, the string "*t" searches for all names in ending with the letter "t". searchPattern 字符串 "s*" 搜索 path 以字母 "s" 开头的所有名称。The searchPattern string "s*" searches for all names in path beginning with the letter "s".

EnumerateFilesGetFiles 方法的区别如下:The EnumerateFiles and GetFiles methods differ as follows:

  • 使用 EnumerateFiles时,可以在返回整个集合之前,开始枚举 FileInfo 对象的集合。When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

  • 使用 GetFiles时,必须等待返回整个 FileInfo 对象数组,然后才能访问数组。When you use GetFiles, you must wait for the whole array of FileInfo objects 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.

如果 DirectoryInfo中没有文件,则此方法返回一个空数组。If there are no files in the DirectoryInfo, this method returns an empty array.

searchPattern 参数中允许使用以下通配符说明符。The following wildcard specifiers are permitted in the searchPattern parameter.

通配符Wildcard character 说明Description
* 零个或多个字符。Zero or more characters.
?? 正好为零或一个字符。Exactly zero or one character.

不保证返回的文件名的顺序;如果需要特定的排序顺序,请使用 Sort 方法。The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

允许使用通配符。Wildcards are permitted. 例如,searchPattern 字符串 "* .txt" 搜索扩展名为 "txt" 的所有文件名。For example, the searchPattern string "*.txt" searches for all file names having an extension of "txt". searchPattern 字符串 "s*" 搜索所有以字母 "s" 开头的文件名称。The searchPattern string "s*" searches for all file names beginning with the letter "s". 如果没有文件,或者没有与 DirectoryInfo中的 searchPattern 字符串匹配的文件,则此方法将返回一个空数组。If there are no files, or no files that match the searchPattern string in the DirectoryInfo, this method returns an empty array.

备注

searchPattern 中使用星号通配符(例如 "* .txt")时,匹配行为因指定文件扩展名的长度而异。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. 文件扩展名正好为三个字符的 searchPattern 返回扩展名为三个或更多字符的文件,其中前三个字符与 searchPattern中指定的文件扩展名相匹配。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. 文件扩展名为一个、两个或三个以上的字符的 searchPattern 仅返回扩展名正好与 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. 例如,如果一个目录中有两个文件,"file1" 和 "txtother",则为 "file?" 的搜索模式。txt "仅返回第一个文件,而" file*.txt "的搜索模式返回这两个文件。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.

备注

由于此方法使用8.3 文件名格式和长文件名格式的文件名进行检查,因此类似于 "* 1*.txt" 的搜索模式可能会返回意外的文件名。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. 例如,使用搜索模式 "*1*.txt" 将返回 "longfilename",因为等效的8.3 文件名格式为 "longf ~ 1 .txt"。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".

此方法预先填充以下 FileInfo 属性的值:This method pre-populates the values of the following FileInfo properties:

另请参阅

适用于