作者:Meloux | 来源:互联网 | 2024-10-26 16:05
本文详细解析了`DirectoryInfo.GetFiles`方法的使用方法及其应用场景。通过示例代码展示了如何在C#程序中利用该方法获取指定目录下的所有文件列表,同时探讨了其参数选项和返回值类型,为开发者提供了实用的操作指南。
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 . . .
*/
DictoryInfo.GetFiles,布布扣,bubuko.com