How do I search for a list of files using wildcard

后端 未结 3 752
無奈伤痛
無奈伤痛 2020-12-01 23:24

How do I use wildcards in C# to list down files contained in a selected folder?

相关标签:
3条回答
  • 2020-12-01 23:46

    You can do something like this:

    string[] files = Directory.GetFiles(@"c:\myfolder", "*.txt", SearchOption.AllDirectories)
    
    0 讨论(0)
  • 2020-12-01 23:50

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-01 23:54

    Directory.GetFiles is your friend here:

    Directory.GetFiles(@"C:\Users\Me\Documents", "*.docx");
    

    or, recursively:

    Directory.GetFiles(
        @"C:\Users\Me\Documents",
        "*.docx",
        SearchOption.AllDirectories);
    
    0 讨论(0)
提交回复
热议问题