带有遮罩的C#Directory.GetFiles [英] C# Directory.GetFiles with mask
本文介绍了带有遮罩的C#Directory.GetFiles的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#中,我想从与以下掩码匹配的特定目录中获取所有文件:
In C#, I would like to get all files from a specific directory that matches the following mask:
- 前缀为
"myfile_"
- 后缀是一些数字
- 文件扩展名是
xml
- prefix is
"myfile_"
- suffix is some numeric number
- file extension is
xml
即
myfile_4.xml
myfile_24.xml
以下文件不应与掩码匹配:
the following files should not match the mask:
_myfile_6.xml
myfile_6.xml_
代码应该对此有所帮助(也许某些linq查询可以提供帮助)
the code should like somehing this this (maybe some linq query can help)
string[] files = Directory.GetFiles(folder, "???");
谢谢
推荐答案
我对正则表达式不好,但这可能有帮助-
I am not good with regular expressions, but this might help -
var myFiles = from file in System.IO.Directory.GetFiles(folder, "myfile_*.xml")
where Regex.IsMatch(file, "myfile_[0-9]+.xml",RegexOptions.IgnoreCase) //use the correct regex here
select file;
这篇关于带有遮罩的C#Directory.GetFiles的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文