Directory.GetFileSystemEntries 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回满足指定条件的所有文件和子目录的名称。
重载
| 名称 | 说明 |
|---|---|
| GetFileSystemEntries(String) |
返回指定路径中所有文件和子目录的名称。 |
| GetFileSystemEntries(String, String) |
返回与指定路径中的搜索模式匹配的文件名和目录名称数组。 |
| GetFileSystemEntries(String, String, EnumerationOptions) |
返回与指定路径中的搜索模式和枚举选项匹配的文件名和目录名称数组。 |
| GetFileSystemEntries(String, String, SearchOption) |
返回与指定路径中的搜索模式匹配的所有文件名和目录名称的数组,并选择性地搜索子目录。 |
GetFileSystemEntries(String)
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
返回指定路径中所有文件和子目录的名称。
public:
static cli::array <System::String ^> ^ GetFileSystemEntries(System::String ^ path);
public static string[] GetFileSystemEntries(string path);
static member GetFileSystemEntries : string -> string[]
Public Shared Function GetFileSystemEntries (path As String) As String()
参数
- path
- String
要搜索的目录的相对路径或绝对路径。 此字符串不区分大小写。
返回
指定目录中的文件和子目录的名称数组,如果未找到任何文件或子目录,则为空数组。
例外
调用方没有所需的权限。
低于 2.1 的 .NET Framework 和 .NET Core 版本: path 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用 < a0/> 查询无效字符
path 是 null。
指定的路径、文件名或两者都超过了系统定义的最大长度。
path 是文件名。
指定的路径无效(例如,它位于未映射的驱动器上)。
示例
以下示例使用 GetFileSystemEntries 该方法填充字符串数组,其中包含用户指定位置中所有文件和子目录的名称,并将数组中的每个字符串打印到控制台。 此示例配置为捕获此方法通用的所有错误。
using System;
namespace GetFileSystemEntries
{
class Class1
{
static void Main(string[] args)
{
Class1 snippets = new Class1();
string path = System.IO.Directory.GetCurrentDirectory();
string filter = "*.exe";
snippets.PrintFileSystemEntries(path);
snippets.PrintFileSystemEntries(path, filter);
snippets.GetLogicalDrives();
snippets.GetParent(path);
snippets.Move("C:\\proof", "C:\\Temp");
}
void PrintFileSystemEntries(string path)
{
try
{
// Obtain the file system entries in the directory path.
string[] directoryEntries =
System.IO.Directory.GetFileSystemEntries(path);
foreach (string str in directoryEntries)
{
System.Console.WriteLine(str);
}
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, " +
"or contains invalid characters.");
}
catch (System.IO.DirectoryNotFoundException)
{
System.Console.WriteLine("The path encapsulated in the " +
"Directory object does not exist.");
}
}
void PrintFileSystemEntries(string path, string pattern)
{
try
{
// Obtain the file system entries in the directory
// path that match the pattern.
string[] directoryEntries =
System.IO.Directory.GetFileSystemEntries(path, pattern);
foreach (string str in directoryEntries)
{
System.Console.WriteLine(str);
}
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, " +
"or contains invalid characters.");
}
catch (System.IO.DirectoryNotFoundException)
{
System.Console.WriteLine("The path encapsulated in the " +
"Directory object does not exist.");
}
}
// Print out all logical drives on the system.
void GetLogicalDrives()
{
try
{
string[] drives = System.IO.Directory.GetLogicalDrives();
foreach (string str in drives)
{
System.Console.WriteLine(str);
}
}
catch (System.IO.IOException)
{
System.Console.WriteLine("An I/O error occurs.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
}
void GetParent(string path)
{
try
{
System.IO.DirectoryInfo directoryInfo =
System.IO.Directory.GetParent(path);
System.Console.WriteLine(directoryInfo.FullName);
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, or " +
"contains invalid characters.");
}
}
void Move(string sourcePath, string destinationPath)
{
try
{
System.IO.Directory.Move(sourcePath, destinationPath);
System.Console.WriteLine("The directory move is complete.");
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, " +
"or contains invalid characters.");
}
catch (System.IO.IOException)
{
System.Console.WriteLine("An attempt was made to move a " +
"directory to a different " +
"volume, or destDirName " +
"already exists.");
}
}
}
}
open System
open System.IO
open System.Security
let printFileSystemEntries path =
try
// Obtain the file system entries in the directory path.
let directoryEntries = Directory.GetFileSystemEntries path
for str in directoryEntries do
printfn $"{str}"
with
| :? ArgumentNullException ->
printfn "Path is a null reference."
| :? SecurityException ->
printfn $"The caller does not have the required permission."
| :? ArgumentException ->
printfn $"Path is an empty string, contains only white spaces, or contains invalid characters."
| :? DirectoryNotFoundException ->
printfn $"The path encapsulated in the Directory object does not exist."
let printFileSystemEntriesPattern path pattern =
try
// Obtain the file system entries in the directory
// path that match the pattern.
let directoryEntries = Directory.GetFileSystemEntries(path, pattern)
for str in directoryEntries do
printfn $"{str}"
with
| :? ArgumentNullException -> printfn "Path is a null reference."
| :? SecurityException -> printfn "The caller does not have the required permission."
| :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
| :? DirectoryNotFoundException -> printfn "The path encapsulated in the Directory object does not exist."
// Print out all logical drives on the system.
let getLogicalDrives () =
try
let drives = Directory.GetLogicalDrives()
for str in drives do
printfn $"{str}"
with
| :? IOException -> printfn "An I/O error occurs."
| :? SecurityException -> printfn "The caller does not have the required permission."
let getParent path =
try
let directoryInfo = Directory.GetParent path
printfn $"{directoryInfo.FullName}"
with
| :? ArgumentNullException -> printfn "Path is a null reference."
| :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
let move sourcePath destinationPath =
try
Directory.Move(sourcePath, destinationPath)
printfn "The directory move is complete."
with
| :? ArgumentNullException -> printfn "Path is a null reference."
| :? SecurityException -> printfn "The caller does not have the required permission."
| :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
| :? IOException -> printfn "An attempt was made to move a directory to a different volume, or destDirName already exists."
let path = Directory.GetCurrentDirectory()
let filter = "*.exe"
printFileSystemEntries path
printFileSystemEntriesPattern path filter
getLogicalDrives ()
getParent path
move "C:\\proof" "C:\\Temp"
Option Explicit On
Option Strict On
Namespace GetFileSystemEntries
Class Class1
Overloads Shared Sub Main(ByVal args() As String)
Dim snippets As New Class1()
Dim path As String = System.IO.Directory.GetCurrentDirectory()
Dim filter As String = "*.exe"
snippets.PrintFileSystemEntries(path)
snippets.PrintFileSystemEntries(path, filter)
snippets.GetLogicalDrives()
snippets.GetParent(path)
snippets.Move("C:\proof", "C:\Temp")
End Sub
Sub PrintFileSystemEntries(ByVal path As String)
Try
' Obtain the file system entries in the directory path.
Dim directoryEntries As String()
directoryEntries = System.IO.Directory.GetFileSystemEntries(path)
Dim str As String
For Each str In directoryEntries
System.Console.WriteLine(str)
Next str
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, " + _
"or contains invalid characters.")
Catch exp As System.IO.DirectoryNotFoundException
System.Console.WriteLine("The path encapsulated in the " + _
"Directory object does not exist.")
End Try
End Sub
Sub PrintFileSystemEntries(ByVal path As String, _
ByVal pattern As String)
Try
' Obtain the file system entries in the directory
' path that match the pattern.
Dim directoryEntries As String()
directoryEntries = _
System.IO.Directory.GetFileSystemEntries(path, pattern)
Dim str As String
For Each str In directoryEntries
System.Console.WriteLine(str)
Next str
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, " + _
"or contains invalid characters.")
Catch exp As System.IO.DirectoryNotFoundException
System.Console.WriteLine("The path encapsulated in the " + _
"Directory object does not exist.")
End Try
End Sub
' Print out all logical drives on the system.
Sub GetLogicalDrives()
Try
Dim drives As String()
drives = System.IO.Directory.GetLogicalDrives()
Dim str As String
For Each str In drives
System.Console.WriteLine(str)
Next str
Catch exp As System.IO.IOException
System.Console.WriteLine("An I/O error occurs.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
End Try
End Sub
Sub GetParent(ByVal path As String)
Try
Dim directoryInfo As System.IO.DirectoryInfo
directoryInfo = System.IO.Directory.GetParent(path)
System.Console.WriteLine(directoryInfo.FullName)
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, or " + _
"contains invalid characters.")
End Try
End Sub
Sub Move(ByVal sourcePath As String, ByVal destinationPath As String)
Try
System.IO.Directory.Move(sourcePath, destinationPath)
System.Console.WriteLine("The directory move is complete.")
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, " + _
"or contains invalid characters.")
Catch exp As System.IO.IOException
System.Console.WriteLine("An attempt was made to move a " + _
"directory to a different " + _
"volume, or destDirName " + _
"already exists.")
End Try
End Sub
End Class
End Namespace
注解
不保证返回的文件和目录名称的顺序; Sort 如果需要特定的排序顺序,请使用该方法。
方法EnumerateFileSystemEntriesGetFileSystemEntries不同,如下所示:使用EnumerateFileSystemEntries时,可以在返回整个集合之前开始枚举条目的集合;使用GetFileSystemEntries时,必须先等待返回整个条目数组,然后才能访问数组。 因此,处理许多文件和目录时, EnumerateFileSystemEntries 可以更高效。
此方法与 GetFileSystemEntries 星号 \ 指定为搜索模式相同。
允许参数 path 指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
参数的 path 区分大小写对应于运行代码的文件系统。 例如,它在 NTFS(默认 Windows 文件系统)上不区分大小写,在 Linux 文件系统上区分大小写。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
GetFileSystemEntries(String, String)
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
返回与指定路径中的搜索模式匹配的文件名和目录名称数组。
public:
static cli::array <System::String ^> ^ GetFileSystemEntries(System::String ^ path, System::String ^ searchPattern);
public static string[] GetFileSystemEntries(string path, string searchPattern);
static member GetFileSystemEntries : string * string -> string[]
Public Shared Function GetFileSystemEntries (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.
指定的路径、文件名或两者都超过了系统定义的最大长度。
path 是文件名。
指定的路径无效(例如,它位于未映射的驱动器上)。
示例
以下示例使用 GetFileSystemEntries 该方法填充字符串数组,其中包含与特定位置中用户指定的筛选器匹配的所有文件的名称,并将数组中的每个字符串打印到控制台。 此示例配置为捕获此方法通用的所有错误。
using System;
namespace GetFileSystemEntries
{
class Class1
{
static void Main(string[] args)
{
Class1 snippets = new Class1();
string path = System.IO.Directory.GetCurrentDirectory();
string filter = "*.exe";
snippets.PrintFileSystemEntries(path);
snippets.PrintFileSystemEntries(path, filter);
snippets.GetLogicalDrives();
snippets.GetParent(path);
snippets.Move("C:\\proof", "C:\\Temp");
}
void PrintFileSystemEntries(string path)
{
try
{
// Obtain the file system entries in the directory path.
string[] directoryEntries =
System.IO.Directory.GetFileSystemEntries(path);
foreach (string str in directoryEntries)
{
System.Console.WriteLine(str);
}
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, " +
"or contains invalid characters.");
}
catch (System.IO.DirectoryNotFoundException)
{
System.Console.WriteLine("The path encapsulated in the " +
"Directory object does not exist.");
}
}
void PrintFileSystemEntries(string path, string pattern)
{
try
{
// Obtain the file system entries in the directory
// path that match the pattern.
string[] directoryEntries =
System.IO.Directory.GetFileSystemEntries(path, pattern);
foreach (string str in directoryEntries)
{
System.Console.WriteLine(str);
}
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, " +
"or contains invalid characters.");
}
catch (System.IO.DirectoryNotFoundException)
{
System.Console.WriteLine("The path encapsulated in the " +
"Directory object does not exist.");
}
}
// Print out all logical drives on the system.
void GetLogicalDrives()
{
try
{
string[] drives = System.IO.Directory.GetLogicalDrives();
foreach (string str in drives)
{
System.Console.WriteLine(str);
}
}
catch (System.IO.IOException)
{
System.Console.WriteLine("An I/O error occurs.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
}
void GetParent(string path)
{
try
{
System.IO.DirectoryInfo directoryInfo =
System.IO.Directory.GetParent(path);
System.Console.WriteLine(directoryInfo.FullName);
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, or " +
"contains invalid characters.");
}
}
void Move(string sourcePath, string destinationPath)
{
try
{
System.IO.Directory.Move(sourcePath, destinationPath);
System.Console.WriteLine("The directory move is complete.");
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, " +
"or contains invalid characters.");
}
catch (System.IO.IOException)
{
System.Console.WriteLine("An attempt was made to move a " +
"directory to a different " +
"volume, or destDirName " +
"already exists.");
}
}
}
}
open System
open System.IO
open System.Security
let printFileSystemEntries path =
try
// Obtain the file system entries in the directory path.
let directoryEntries = Directory.GetFileSystemEntries path
for str in directoryEntries do
printfn $"{str}"
with
| :? ArgumentNullException ->
printfn "Path is a null reference."
| :? SecurityException ->
printfn $"The caller does not have the required permission."
| :? ArgumentException ->
printfn $"Path is an empty string, contains only white spaces, or contains invalid characters."
| :? DirectoryNotFoundException ->
printfn $"The path encapsulated in the Directory object does not exist."
let printFileSystemEntriesPattern path pattern =
try
// Obtain the file system entries in the directory
// path that match the pattern.
let directoryEntries = Directory.GetFileSystemEntries(path, pattern)
for str in directoryEntries do
printfn $"{str}"
with
| :? ArgumentNullException -> printfn "Path is a null reference."
| :? SecurityException -> printfn "The caller does not have the required permission."
| :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
| :? DirectoryNotFoundException -> printfn "The path encapsulated in the Directory object does not exist."
// Print out all logical drives on the system.
let getLogicalDrives () =
try
let drives = Directory.GetLogicalDrives()
for str in drives do
printfn $"{str}"
with
| :? IOException -> printfn "An I/O error occurs."
| :? SecurityException -> printfn "The caller does not have the required permission."
let getParent path =
try
let directoryInfo = Directory.GetParent path
printfn $"{directoryInfo.FullName}"
with
| :? ArgumentNullException -> printfn "Path is a null reference."
| :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
let move sourcePath destinationPath =
try
Directory.Move(sourcePath, destinationPath)
printfn "The directory move is complete."
with
| :? ArgumentNullException -> printfn "Path is a null reference."
| :? SecurityException -> printfn "The caller does not have the required permission."
| :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
| :? IOException -> printfn "An attempt was made to move a directory to a different volume, or destDirName already exists."
let path = Directory.GetCurrentDirectory()
let filter = "*.exe"
printFileSystemEntries path
printFileSystemEntriesPattern path filter
getLogicalDrives ()
getParent path
move "C:\\proof" "C:\\Temp"
Option Explicit On
Option Strict On
Namespace GetFileSystemEntries
Class Class1
Overloads Shared Sub Main(ByVal args() As String)
Dim snippets As New Class1()
Dim path As String = System.IO.Directory.GetCurrentDirectory()
Dim filter As String = "*.exe"
snippets.PrintFileSystemEntries(path)
snippets.PrintFileSystemEntries(path, filter)
snippets.GetLogicalDrives()
snippets.GetParent(path)
snippets.Move("C:\proof", "C:\Temp")
End Sub
Sub PrintFileSystemEntries(ByVal path As String)
Try
' Obtain the file system entries in the directory path.
Dim directoryEntries As String()
directoryEntries = System.IO.Directory.GetFileSystemEntries(path)
Dim str As String
For Each str In directoryEntries
System.Console.WriteLine(str)
Next str
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, " + _
"or contains invalid characters.")
Catch exp As System.IO.DirectoryNotFoundException
System.Console.WriteLine("The path encapsulated in the " + _
"Directory object does not exist.")
End Try
End Sub
Sub PrintFileSystemEntries(ByVal path As String, _
ByVal pattern As String)
Try
' Obtain the file system entries in the directory
' path that match the pattern.
Dim directoryEntries As String()
directoryEntries = _
System.IO.Directory.GetFileSystemEntries(path, pattern)
Dim str As String
For Each str In directoryEntries
System.Console.WriteLine(str)
Next str
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, " + _
"or contains invalid characters.")
Catch exp As System.IO.DirectoryNotFoundException
System.Console.WriteLine("The path encapsulated in the " + _
"Directory object does not exist.")
End Try
End Sub
' Print out all logical drives on the system.
Sub GetLogicalDrives()
Try
Dim drives As String()
drives = System.IO.Directory.GetLogicalDrives()
Dim str As String
For Each str In drives
System.Console.WriteLine(str)
Next str
Catch exp As System.IO.IOException
System.Console.WriteLine("An I/O error occurs.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
End Try
End Sub
Sub GetParent(ByVal path As String)
Try
Dim directoryInfo As System.IO.DirectoryInfo
directoryInfo = System.IO.Directory.GetParent(path)
System.Console.WriteLine(directoryInfo.FullName)
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, or " + _
"contains invalid characters.")
End Try
End Sub
Sub Move(ByVal sourcePath As String, ByVal destinationPath As String)
Try
System.IO.Directory.Move(sourcePath, destinationPath)
System.Console.WriteLine("The directory move is complete.")
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, " + _
"or contains invalid characters.")
Catch exp As System.IO.IOException
System.Console.WriteLine("An attempt was made to move a " + _
"directory to a different " + _
"volume, or destDirName " + _
"already exists.")
End Try
End Sub
End Class
End Namespace
注解
不保证返回的文件和目录名称的顺序; Sort 如果需要特定的排序顺序,请使用该方法。
searchPattern 可以是文本字符和通配符的组合,但它不支持正则表达式。 允许在以下通配符说明符中 searchPattern。
| 通配符说明符 | Matches |
|---|---|
| * (星号) | 该位置中的零个或多个字符。 |
| ? (问号) | 该位置正好有一个字符。 |
通配符以外的字符是文本字符。 例如, searchPattern 字符串“*t”搜索以字母“t”结尾的所有名称 path 。
searchPattern字符串“s*”搜索以字母“s”开头的所有名称path。
searchPattern不能以两个句点(“..”)结尾,或者包含两个句点(“..”)后跟DirectorySeparatorCharAltDirectorySeparatorChar,也不能包含任何无效字符。 可以使用该方法 GetInvalidPathChars 查询无效字符。
注释
在“*.txt”等中使用星号通配符 searchPattern 时,指定扩展中的字符数会影响搜索,如下所示:
- 如果指定的扩展名正好是三个字符长,该方法将返回扩展名以指定扩展名开头的文件。 例如,“*.xls”同时返回“book.xls”和“book.xlsx”。
- 在所有其他情况下,该方法返回与指定扩展名完全匹配的文件。 例如,“*.ai”返回“file.ai”,但返回“file.aif”。
使用问号通配符时,此方法仅返回与指定文件扩展名匹配的文件。 例如,给定两个文件“file1.txt”和“file1.txtother”,在目录中,搜索模式为“file?.txt“仅返回第一个文件,而”file*.txt“的搜索模式将返回这两个文件。
允许参数 path 指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
参数的 path 区分大小写对应于运行代码的文件系统。 例如,它在 NTFS(默认 Windows 文件系统)上不区分大小写,在 Linux 文件系统上区分大小写。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
GetFileSystemEntries(String, String, EnumerationOptions)
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
返回与指定路径中的搜索模式和枚举选项匹配的文件名和目录名称数组。
public:
static cli::array <System::String ^> ^ GetFileSystemEntries(System::String ^ path, System::String ^ searchPattern, System::IO::EnumerationOptions ^ enumerationOptions);
public static string[] GetFileSystemEntries(string path, string searchPattern, System.IO.EnumerationOptions enumerationOptions);
static member GetFileSystemEntries : string * string * System.IO.EnumerationOptions -> string[]
Public Shared Function GetFileSystemEntries (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 查询无效字符。
注释
在“*.txt”等中使用星号通配符 searchPattern 时,指定扩展中的字符数会影响搜索,如下所示:
- 如果指定的扩展名正好是三个字符长,该方法将返回扩展名以指定扩展名开头的文件。 例如,“*.xls”同时返回“book.xls”和“book.xlsx”。
- 在所有其他情况下,该方法返回与指定扩展名完全匹配的文件。 例如,“*.ai”返回“file.ai”,但返回“file.aif”。
使用问号通配符时,此方法仅返回与指定文件扩展名匹配的文件。 例如,给定两个文件“file1.txt”和“file1.txtother”,在目录中,搜索模式为“file?.txt“仅返回第一个文件,而”file*.txt“的搜索模式将返回这两个文件。
允许参数 path 指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
参数的 path 区分大小写对应于运行代码的文件系统。 例如,它在 NTFS(默认 Windows 文件系统)上不区分大小写,在 Linux 文件系统上区分大小写。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
适用于
GetFileSystemEntries(String, String, SearchOption)
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
返回与指定路径中的搜索模式匹配的所有文件名和目录名称的数组,并选择性地搜索子目录。
public:
static cli::array <System::String ^> ^ GetFileSystemEntries(System::String ^ path, System::String ^ searchPattern, System::IO::SearchOption searchOption);
public static string[] GetFileSystemEntries(string path, string searchPattern, System.IO.SearchOption searchOption);
static member GetFileSystemEntries : string * string * System.IO.SearchOption -> string[]
Public Shared Function GetFileSystemEntries (path As String, searchPattern As String, searchOption As SearchOption) As String()
参数
- path
- String
要搜索的目录的相对路径或绝对路径。 此字符串不区分大小写。
- searchPattern
- String
要与中 path文件和目录的名称匹配的搜索字符串。 此参数可以包含有效文本路径和通配符 (* 和 ?) 字符的组合,但它不支持正则表达式。
- searchOption
- SearchOption
枚举值之一,指定搜索操作是否应仅包括当前目录,还是应包含所有子目录。 默认值为 TopDirectoryOnly。
返回
文件名和目录名称与指定搜索条件匹配的文件数组,如果未找到任何文件或目录,则为空数组。
例外
.NET Framework 和低于 2.1 的核心版本:.NET path是长度为零的字符串,仅包含空格或包含无效字符。 可以使用该方法 GetInvalidPathChars() 查询无效字符。
-或-
searchPattern 不包含有效的模式。
searchOption 不是有效 SearchOption 值。
path 无效,例如引用未映射的驱动器。
path 是文件名。
指定的路径、文件名或组合超过了系统定义的最大长度。
调用方没有所需的权限。
调用方没有所需的权限。
注解
不保证返回的文件和目录名称的顺序; Sort 如果需要特定的排序顺序,请使用该方法。
searchPattern 可以是文本字符和通配符的组合,但它不支持正则表达式。 允许在以下通配符说明符中 searchPattern。
| 通配符说明符 | Matches |
|---|---|
| * (星号) | 该位置中的零个或多个字符。 |
| ? (问号) | 该位置正好有一个字符。 |
通配符以外的字符是文本字符。 例如, searchPattern 字符串“*t”搜索以字母“t”结尾的所有名称 path 。
searchPattern字符串“s*”搜索以字母“s”开头的所有名称path。
searchPattern不能以两个句点(“..”)结尾,或者包含两个句点(“..”)后跟DirectorySeparatorCharAltDirectorySeparatorChar,也不能包含任何无效字符。 可以使用该方法 GetInvalidPathChars 查询无效字符。
注释
在“*.txt”等中使用星号通配符 searchPattern 时,指定扩展中的字符数会影响搜索,如下所示:
- 如果指定的扩展名正好是三个字符长,该方法将返回扩展名以指定扩展名开头的文件。 例如,“*.xls”同时返回“book.xls”和“book.xlsx”。
- 在所有其他情况下,该方法返回与指定扩展名完全匹配的文件。 例如,“*.ai”返回“file.ai”,但返回“file.aif”。
使用问号通配符时,此方法仅返回与指定文件扩展名匹配的文件。 例如,给定两个文件“file1.txt”和“file1.txtother”,在目录中,搜索模式为“file?.txt“仅返回第一个文件,而”file*.txt“的搜索模式将返回这两个文件。
方法EnumerateFileSystemEntriesGetFileSystemEntries不同,如下所示:使用EnumerateFileSystemEntries时,可以在返回整个集合之前开始枚举条目的集合;使用GetFileSystemEntries时,必须先等待返回整个条目数组,然后才能访问数组。 因此,处理许多文件和目录时, EnumerateFileSystemEntries 可以更高效。
可以使用参数指定相对路径信息 path 。 相对路径信息被解释为相对于当前工作目录,可以使用该方法来确定该 GetCurrentDirectory 目录。