How do I search for a list of files using wildcard
How do I use wildcards in C# to list down files contained 开发者_开发技巧in a selected folder?
Directory.GetFiles
is your friend here:
Directory.GetFiles(@"C:\Users\Me\Documents", "*.docx");
or, recursively:
Directory.GetFiles(
@"C:\Users\Me\Documents",
"*.docx",
SearchOption.AllDirectories);
Use DirectoryInfo.GetFiles
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);
}
}
You can do something like this:
string[] files = Directory.GetFiles(@"c:\myfolder", "*.txt", SearchOption.AllDirectories)
精彩评论