使用Directory.GetFiles在C#正则表达式? [英] Using Directory.GetFiles with a regex in C#?

查看:316
本文介绍了使用Directory.GetFiles在C#正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

string[] files = Directory.GetFiles(path, "......", SearchOption.AllDirectories)

我要的是仅返回不与<$开头的文件C $ç>点_ 和 T _ 并具有扩展名PNG或JPG或GIF。我会怎么做呢?

What I want is to return only files which do NOT start with p_ and t_ and have the extension png or jpg or gif. How would I do this?

推荐答案

Directory.GetFiles 不支持正则表达式默认情况下,你可以做的就是正则表达式您的文件列表进行过滤。看看此房源:

Directory.GetFiles doesn't support RegEx by default, what you can do is to filter by RegEx on your file list. Take a look at this listing:

Regex reg = new Regex(@"^^(?!p_|t_).*");

var files = Directory.GetFiles(yourPath, "*.png; *.jpg; *.gif")
                     .Where(path => reg.IsMatch(path))
                     .ToList();

这篇关于使用Directory.GetFiles在C#正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆