directory.getfiles searchPattern
时间: 2024-03-21 22:39:50 浏览: 13
`Directory.GetFiles` 方法的第二个参数 `searchPattern` 是一个字符串,用于指定搜索文件的模式。这个模式可以包含通配符,例如:
- `*.txt`:获取所有以 ".txt" 结尾的文件。
- `*.*`:获取目录中的所有文件。
- `file?.txt`:获取以 "file" 开头,后面跟着一个任意字符,最后以 ".txt" 结尾的文件,例如 "file1.txt"、"fileA.txt" 等。
你也可以将多个搜索模式组合在一起,使用分号分隔。例如:
```
Directory.GetFiles(directoryPath, "*.txt;*.doc");
```
这个代码将获取所有以 ".txt" 或 ".doc" 结尾的文件。
相关问题
Directory.GetFiles
Directory.GetFiles is a method in the .NET Framework that returns an array of file names in the specified directory that match the specified search pattern.
Syntax:
```
public static string[] GetFiles(string path, string searchPattern);
```
Parameters:
- path: the path to the directory to search
- searchPattern: the search pattern to match against the file names in the directory. This can include wildcards such as "*" and "?".
Return value:
- An array of file names that match the specified search pattern in the specified directory.
Example usage:
```
string[] files = Directory.GetFiles(@"C:\Users\Username\Documents", "*.txt");
foreach (string file in files)
{
Console.WriteLine(file);
}
```
This code will search for all files with a .txt extension in the user's Documents folder and print out the file names.
directory.getfiles多类型
你可以使用通配符来获取多种类型的文件。例如,如果你想获取所有的文本文件和图像文件,你可以这样写:
```
string[] fileTypes = { "*.txt", "*.doc", "*.jpg", "*.png" };
List<string> files = new List<string>();
foreach (string fileType in fileTypes)
{
files.AddRange(Directory.GetFiles(directoryPath, fileType));
}
```
这个代码将使用 `Directory.GetFiles` 方法来获取所有的以 ".txt"、".doc"、".jpg" 或 ".png" 结尾的文件,并将它们添加到一个列表中。