I have this code:
string[] files = Directory.GetFiles(path, "......", SearchOption.AllDirectories)
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?
[ VB.NET ] Net Framework will probably have Directory. getFiles method that supports multiple filters.
Text. RegularExpressions namespace is a Regex class, which encapsulates the interface to the regular expressions engine and allows you to perform matches and extract information from text using regular expressions.
C# provides a class termed as Regex which can be found in System. Text. RegularExpression namespace.
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();
You can't stick a Regex into the parameter, it's just a simple string filter. Try using LINQ to filter out afterwards instead.
var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories) .Where(s => s.EndsWith(".jpg") || s.EndsWith(".png")) .Where(s => s.StartsWith("p_") == false && s.StartsWith("t_") == false)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With