如何在C#中使用通配符列出所选文件夹中包含的文件?
答案 0 :(得分:68)
Directory.GetFiles
是你的朋友:
Directory.GetFiles(@"C:\Users\Me\Documents", "*.docx");
或递归:
Directory.GetFiles(
@"C:\Users\Me\Documents",
"*.docx",
SearchOption.AllDirectories);
答案 1 :(得分:17)
using System.IO;
DirectoryInfo folder = new DirectoryInfo(@"C:\foo\bar");
if (folder.Exists) // else: Invalid folder!
{
FileInfo[] files = folder.GetFiles("*.xml");
foreach (FileInfo file in files)
{
DoSomething(file.FullName);
}
}
答案 2 :(得分:6)
您可以这样做:
string[] files = Directory.GetFiles(@"c:\myfolder", "*.txt", SearchOption.AllDirectories)