Regex to find a file in folder

前端 未结 2 1568
轮回少年
轮回少年 2021-01-07 11:55

How to find all files matches a regex pattern in a folder?

Thanks

相关标签:
2条回答
  • 2021-01-07 12:08

    Regex matching of filesystem is not supported you will have to iterate through each of the files in the directory and check them individually

    0 讨论(0)
  • 2021-01-07 12:10

    The GetFiles method allows you to specify a wildcard pattern but not really a regex. Another possibility is to simply loop through the files and validate their name against a regex.

    IEnumerable<string> files = Directory
        .EnumerateFiles(@"c:\somePath")
        .Where(name => Regex.IsMatch(name, "SOME REGEX"));
    
    0 讨论(0)
提交回复
热议问题