开发者

Using the linq search option in enumerate files

开发者 https://www.devze.com 2023-04-07 13:04 出处:网络
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:

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"));
0

精彩评论

暂无评论...
验证码 换一张
取 消