Using the linq search option in enumerate files
Quick one here. I am trying to EnumerateFiles
in a C# application and I want to find all the files in a directory that do not match a given patter开发者_高级运维n. So I would have something like this:
var files = Directory.EnumerateFiles("MY_DIR_PATH", "NOT_MY_FILE_NAME");
Can someone help me out with the not part?
I don't think you can use that overload of EnumerateFiles for this, but you can use linq:
Directory.EnumerateFiles("MY_DIR_PATH").Where(s => s != "NOT_MY_FILE_NAME");
or in query syntax:
var files = from f in Directory.EnumerateFiles("MY_DIR_PATH")
where f != "NOT_MY_FILE_NAME"
select f;
You can do something like that:
var files = Directory.EnumerateFiles("MY_DIR_PATH")
.Where(fileName => fileName != "MY_FILE_NAME");
How about
var files = Directory.GetFiles("MY_DIR_PATH")
.Where(f => !f.Contains("NOT_MY_FILE_NAME"));
精彩评论