Directory.GetFiles 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回符合指定条件的文件的名称。
重载
| 名称 | 说明 |
|---|---|
| GetFiles(String) |
返回指定目录中的文件名称(包括其路径)。 |
| GetFiles(String, String) |
返回与指定目录中的指定搜索模式匹配的文件的名称(包括其路径)。 |
| GetFiles(String, String, EnumerationOptions) |
返回与指定目录中的指定搜索模式和枚举选项匹配的文件的名称(包括其路径)。 |
| GetFiles(String, String, SearchOption) |
返回与指定目录中的指定搜索模式匹配的文件(包括其路径)的名称,该值用于确定是否搜索子目录。 |
GetFiles(String)
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
返回指定目录中的文件名称(包括其路径)。
public:
static cli::array <System::String ^> ^ GetFiles(System::String ^ path);
public static string[] GetFiles(string path);
static member GetFiles : string -> string[]
Public Shared Function GetFiles (path As String) As String()
参数
- path
- String
要搜索的目录的相对路径或绝对路径。 此字符串不区分大小写。
返回
指定目录中文件的全名数组(包括路径),如果未找到任何文件,则为空数组。
例外
调用方没有所需的权限。
低于 2.1 的 .NET Framework 和 .NET Core 版本: path 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用该方法 GetInvalidPathChars() 查询无效字符。
path 是 null。
指定的路径、文件名或两者都超过了系统定义的最大长度。
找不到指定的路径或无效(例如,它位于未映射的驱动器上)。
示例
以下示例演示如何使用 GetFiles 该方法从用户指定的位置返回文件名。 此示例配置为捕获此方法通用的所有错误。
// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;
public class RecursiveFileProcessor
{
public static void Main(string[] args)
{
foreach(string path in args)
{
if(File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
else
{
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
ProcessFile(fileName);
// Recurse into subdirectories of this directory.
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
// Insert logic for processing found files here.
public static void ProcessFile(string path)
{
Console.WriteLine("Processed file '{0}'.", path);
}
}
module RecursiveFileProcessor
open System.IO
// Insert logic for processing found files here.
let processFile path =
printfn $"Processed file '%s{path}'."
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
let rec processDirectory targetDirectory =
// Process the list of files found in the directory.
let fileEntries = Directory.GetFiles targetDirectory
for fileName in fileEntries do
processFile fileName
// Recurse into subdirectories of this directory.
let subdirectoryEntries = Directory.GetDirectories targetDirectory
for subdirectory in subdirectoryEntries do
processDirectory subdirectory
[<EntryPoint>]
let main args =
for path in args do
if File.Exists path then
// This path is a file
processFile path
elif Directory.Exists path then
// This path is a directory
processDirectory path
else
printfn $"{path} is not a valid file or directory."
0
' For Directory.GetFiles and Directory.GetDirectories
' For File.Exists, Directory.Exists
Imports System.IO
Imports System.Collections
Public Class RecursiveFileProcessor
Public Overloads Shared Sub Main(ByVal args() As String)
Dim path As String
For Each path In args
If File.Exists(path) Then
' This path is a file.
ProcessFile(path)
Else
If Directory.Exists(path) Then
' This path is a directory.
ProcessDirectory(path)
Else
Console.WriteLine("{0} is not a valid file or directory.", path)
End If
End If
Next path
End Sub
' Process all files in the directory passed in, recurse on any directories
' that are found, and process the files they contain.
Public Shared Sub ProcessDirectory(ByVal targetDirectory As String)
Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
' Process the list of files found in the directory.
Dim fileName As String
For Each fileName In fileEntries
ProcessFile(fileName)
Next fileName
Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
' Recurse into subdirectories of this directory.
Dim subdirectory As String
For Each subdirectory In subdirectoryEntries
ProcessDirectory(subdirectory)
Next subdirectory
End Sub
' Insert logic for processing found files here.
Public Shared Sub ProcessFile(ByVal path As String)
Console.WriteLine("Processed file '{0}'.", path)
End Sub
End Class
注解
方法和EnumerateFilesGetFiles方法有所不同:使用EnumerateFiles时,可以在返回整个集合之前开始枚举名称集合;使用GetFiles时,必须先等待返回整个名称数组,然后才能访问数组。 因此,处理许多文件和目录时, EnumerateFiles 可以更高效。
返回的文件名将追加到提供 path 的参数中。
此方法与 GetFiles(String, String) 星号 \ 指定为搜索模式相同。
该 path 参数可以指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
不保证返回文件名的顺序; Sort 如果需要特定的排序顺序,请使用该方法。
参数的 path 区分大小写对应于运行代码的文件系统。 例如,它在 NTFS(默认 Windows 文件系统)上不区分大小写,在 Linux 文件系统上区分大小写。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
GetFiles(String, String)
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
返回与指定目录中的指定搜索模式匹配的文件的名称(包括其路径)。
public:
static cli::array <System::String ^> ^ GetFiles(System::String ^ path, System::String ^ searchPattern);
public static string[] GetFiles(string path, string searchPattern);
static member GetFiles : string * string -> string[]
Public Shared Function GetFiles (path As String, searchPattern As String) As String()
参数
- path
- String
要搜索的目录的相对路径或绝对路径。 此字符串不区分大小写。
- searchPattern
- String
要与文件中 path的名称匹配的搜索字符串。 此参数可以包含有效文本路径和通配符 (* 和 ?) 字符的组合,但它不支持正则表达式。
返回
指定目录中与指定搜索模式匹配的文件的完整名称(包括路径)的数组;如果未找到任何文件,则为空数组。
例外
调用方没有所需的权限。
低于 2.1 的 .NET Framework 和 .NET Core 版本: path 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用 查询无效字符 GetInvalidPathChars()。
-或-
searchPattern 不包含有效的模式。
path 或 searchPattern 为 null.
指定的路径、文件名或两者都超过了系统定义的最大长度。
找不到指定的路径或无效(例如,它位于未映射的驱动器上)。
示例
以下示例计算以指定字母开头的文件数。
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
// Only get files that begin with the letter "c".
string[] dirs = Directory.GetFiles(@"c:\", "c*");
Console.WriteLine("The number of files starting with c is {0}.", dirs.Length);
foreach (string dir in dirs)
{
Console.WriteLine(dir);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
open System.IO
try
// Only get files that begin with the letter "c".
let dirs = Directory.GetFiles(@"c:\", "c*")
printfn $"The number of files starting with c is {dirs.Length}."
for dir in dirs do
printfn $"{dir}"
with e ->
printfn $"The process failed: {e}"
Imports System.IO
Public Class Test
Public Shared Sub Main()
Try
' Only get files that begin with the letter "c".
Dim dirs As String() = Directory.GetFiles("c:\", "c*")
Console.WriteLine("The number of files starting with c is {0}.", dirs.Length)
Dim dir As String
For Each dir In dirs
Console.WriteLine(dir)
Next
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
注解
返回的文件名将追加到提供 path 的参数中,并且返回的文件名的顺序是不能保证的;如果需要特定的排序顺序, Sort 请使用该方法。
searchPattern 可以是文本字符和通配符的组合,但它不支持正则表达式。 允许在以下通配符说明符中 searchPattern。
| 通配符说明符 | Matches |
|---|---|
| * (星号) | 该位置中的零个或多个字符。 |
| ? (问号) | 该位置正好有一个字符。 |
通配符以外的字符是文本字符。 例如, searchPattern 字符串“*t”搜索以字母“t”结尾的所有名称 path 。
searchPattern字符串“s*”搜索以字母“s”开头的所有名称path。
searchPattern不能以两个句点(“..”)结尾,或者包含两个句点(“..”)后跟DirectorySeparatorCharAltDirectorySeparatorChar,也不能包含任何无效字符。 可以使用该方法 GetInvalidPathChars 查询无效字符。
注释
.NET Framework only: 当你在 searchPattern 中使用星号通配符时,并且指定了三个字符的文件扩展名(例如“*.txt”)时,此方法还返回扩展名为 begin的文件。 例如,搜索模式“*.xls”返回“book.xls”和“book.xlsx”。 仅当搜索模式中使用星号且提供的文件扩展名正好是三个字符时,才会发生此行为。 如果在搜索模式中某个位置使用问号通配符,此方法仅返回与指定文件扩展名完全匹配的文件。 下表描述了 .NET Framework 中的此异常。
| 目录中的文件 | 搜索模式 | .NET 5 个以上的返回 | .NET Framework 返回 |
|---|---|---|---|
| file.ai,file.aif | *。艾 | file.ai | file.ai |
| book.xls,book.xlsx | *.xls | book.xls | book.xls、book.xlsx |
| ello.txt、hello.txt、hello.txtt | ?ello.txt | hello.txt | hello.txt |
注释
由于此方法检查文件名格式为 8.3 和长文件名格式的文件名,因此类似于“*1*.txt”的搜索模式可能会返回意外的文件名。 例如,使用搜索模式“*1*.txt”返回“longfilename.txt”,因为等效的 8.3 文件名格式为“LONGFI~1.TXT”。
方法和EnumerateFilesGetFiles方法有所不同:使用EnumerateFiles时,可以在返回整个集合之前开始枚举名称集合;使用GetFiles时,必须先等待返回整个名称数组,然后才能访问数组。 因此,处理许多文件和目录时, EnumerateFiles 可以更高效。
该 path 参数可以指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
参数的 path 区分大小写对应于运行代码的文件系统。 例如,它在 NTFS(默认 Windows 文件系统)上不区分大小写,在 Linux 文件系统上区分大小写。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
GetFiles(String, String, EnumerationOptions)
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
返回与指定目录中的指定搜索模式和枚举选项匹配的文件的名称(包括其路径)。
public:
static cli::array <System::String ^> ^ GetFiles(System::String ^ path, System::String ^ searchPattern, System::IO::EnumerationOptions ^ enumerationOptions);
public static string[] GetFiles(string path, string searchPattern, System.IO.EnumerationOptions enumerationOptions);
static member GetFiles : string * string * System.IO.EnumerationOptions -> string[]
Public Shared Function GetFiles (path As String, searchPattern As String, enumerationOptions As EnumerationOptions) As String()
参数
- path
- String
要搜索的目录的相对路径或绝对路径。 此字符串不区分大小写。
- searchPattern
- String
要与文件中 path的名称匹配的搜索字符串。 此参数可以包含有效文本和通配符的组合,但它不支持正则表达式。
- enumerationOptions
- EnumerationOptions
描述要使用的搜索和枚举配置的对象。
返回
指定目录中与指定的搜索模式和枚举选项匹配的文件的全名(包括路径)的数组,如果未找到任何文件,则为空数组。
例外
调用方没有所需的权限。
低于 2.1 的 .NET Framework 和 .NET Core 版本: path 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用 查询无效字符 GetInvalidPathChars()。
-或-
searchPattern 不包含有效的模式。
path 或 searchPattern 为 null.
指定的路径、文件名或两者都超过了系统定义的最大长度。
找不到指定的路径或无效(例如,它位于未映射的驱动器上)。
注解
返回的文件名将追加到提供 path 的参数中,并且返回的文件名的顺序是不能保证的;如果需要特定的排序顺序, Sort 请使用该方法。
searchPattern 可以是文本字符和通配符的组合,但它不支持正则表达式。 允许在以下通配符说明符中 searchPattern。
| 通配符说明符 | Matches |
|---|---|
| * (星号) | 该位置中的零个或多个字符。 |
| ? (问号) | 该位置正好有一个字符。 |
通配符以外的字符是文本字符。 例如, searchPattern 字符串“*t”搜索以字母“t”结尾的所有名称 path 。
searchPattern字符串“s*”搜索以字母“s”开头的所有名称path。
searchPattern不能以两个句点(“..”)结尾,或者包含两个句点(“..”)后跟DirectorySeparatorCharAltDirectorySeparatorChar,也不能包含任何无效字符。 可以使用该方法 GetInvalidPathChars 查询无效字符。
注释
.NET Framework only: 当你在 searchPattern 中使用星号通配符时,并且指定了三个字符的文件扩展名(例如“*.txt”)时,此方法还返回扩展名为 begin的文件。 例如,搜索模式“*.xls”返回“book.xls”和“book.xlsx”。 仅当搜索模式中使用星号且提供的文件扩展名正好是三个字符时,才会发生此行为。 如果在搜索模式中某个位置使用问号通配符,此方法仅返回与指定文件扩展名完全匹配的文件。 下表描述了 .NET Framework 中的此异常。
| 目录中的文件 | 搜索模式 | .NET 5 个以上的返回 | .NET Framework 返回 |
|---|---|---|---|
| file.ai,file.aif | *。艾 | file.ai | file.ai |
| book.xls,book.xlsx | *.xls | book.xls | book.xls、book.xlsx |
| ello.txt、hello.txt、hello.txtt | ?ello.txt | hello.txt | hello.txt |
注释
由于此方法检查文件名格式为 8.3 和长文件名格式的文件名,因此类似于“*1*.txt”的搜索模式可能会返回意外的文件名。 例如,使用搜索模式“*1*.txt”返回“longfilename.txt”,因为等效的 8.3 文件名格式为“LONGFI~1.TXT”。
方法和EnumerateFilesGetFiles方法有所不同:使用EnumerateFiles时,可以在返回整个集合之前开始枚举名称集合;使用GetFiles时,必须先等待返回整个名称数组,然后才能访问数组。 因此,处理许多文件和目录时, EnumerateFiles 可以更高效。
该 path 参数可以指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
参数的 path 区分大小写对应于运行代码的文件系统。 例如,它在 NTFS(默认 Windows 文件系统)上不区分大小写,在 Linux 文件系统上区分大小写。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
适用于
GetFiles(String, String, SearchOption)
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
返回与指定目录中的指定搜索模式匹配的文件(包括其路径)的名称,该值用于确定是否搜索子目录。
public:
static cli::array <System::String ^> ^ GetFiles(System::String ^ path, System::String ^ searchPattern, System::IO::SearchOption searchOption);
public static string[] GetFiles(string path, string searchPattern, System.IO.SearchOption searchOption);
static member GetFiles : string * string * System.IO.SearchOption -> string[]
Public Shared Function GetFiles (path As String, searchPattern As String, searchOption As SearchOption) As String()
参数
- path
- String
要搜索的目录的相对路径或绝对路径。 此字符串不区分大小写。
- searchPattern
- String
要与文件中 path的名称匹配的搜索字符串。 此参数可以包含有效文本路径和通配符 (* 和 ?) 字符的组合,但它不支持正则表达式。
- searchOption
- SearchOption
枚举值之一,指定搜索操作是应包括所有子目录还是仅包含当前目录。
返回
指定目录中与指定搜索模式和选项匹配的文件的全名(包括路径)数组,如果未找到任何文件,则为空数组。
例外
低于 2.1 的 .NET Framework 和 .NET Core 版本: path 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用该方法查询无效字符 GetInvalidPathChars() 。
-或-
searchPattern 不包含有效的模式。
path 或 searchPattern 为 null.
searchOption 不是有效 SearchOption 值。
调用方没有所需的权限。
找不到指定的路径或无效(例如,它位于未映射的驱动器上)。
指定的路径、文件名或两者都超过了系统定义的最大长度。
注解
返回的文件名将追加到提供的参数 path 中,并且返回的文件名的顺序是不能保证的;如果需要特定的排序顺序, Sort 请使用该方法。
searchPattern 可以是文本字符和通配符的组合,但它不支持正则表达式。 允许在以下通配符说明符中 searchPattern。
| 通配符说明符 | Matches |
|---|---|
| * (星号) | 该位置中的零个或多个字符。 |
| ? (问号) | 该位置正好有一个字符。 |
通配符以外的字符是文本字符。 例如, searchPattern 字符串“*t”搜索以字母“t”结尾的所有名称 path 。
searchPattern字符串“s*”搜索以字母“s”开头的所有名称path。
searchPattern不能以两个句点(“..”)结尾,或者包含两个句点(“..”)后跟DirectorySeparatorCharAltDirectorySeparatorChar,也不能包含任何无效字符。 可以使用该方法 GetInvalidPathChars 查询无效字符。
注释
.NET Framework only: 当你在 searchPattern 中使用星号通配符时,并且指定了三个字符的文件扩展名(例如“*.txt”)时,此方法还返回扩展名为 begin的文件。 例如,搜索模式“*.xls”返回“book.xls”和“book.xlsx”。 仅当搜索模式中使用星号且提供的文件扩展名正好是三个字符时,才会发生此行为。 如果在搜索模式中某个位置使用问号通配符,此方法仅返回与指定文件扩展名完全匹配的文件。 下表描述了 .NET Framework 中的此异常。
| 目录中的文件 | 搜索模式 | .NET 5 个以上的返回 | .NET Framework 返回 |
|---|---|---|---|
| file.ai,file.aif | *。艾 | file.ai | file.ai |
| book.xls,book.xlsx | *.xls | book.xls | book.xls、book.xlsx |
| ello.txt、hello.txt、hello.txtt | ?ello.txt | hello.txt | hello.txt |
注释
由于此方法检查文件名格式为 8.3 和长文件名格式的文件名,因此类似于“*1*.txt”的搜索模式可能会返回意外的文件名。 例如,使用搜索模式“*1*.txt”返回“longfilename.txt”,因为等效的 8.3 文件名格式为“LONGFI~1.TXT”。
方法和EnumerateFilesGetFiles方法有所不同:使用EnumerateFiles时,可以在返回整个集合之前开始枚举名称集合;使用GetFiles时,必须先等待返回整个名称数组,然后才能访问数组。 因此,处理许多文件和目录时, EnumerateFiles 可以更高效。
文件名包括完整路径。
该 path 参数可以指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
参数的 path 区分大小写对应于运行代码的文件系统。 例如,它在 NTFS(默认 Windows 文件系统)上不区分大小写,在 Linux 文件系统上区分大小写。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。