开发者

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜