C#中搜索文件和文件夹,除非在特定的文件夹 [英] C# Searching for files and folders except in certain folders

查看:208
本文介绍了C#中搜索文件和文件夹,除非在特定的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法来排除SearchOption使用LINQ命令这样某些目录

Is there any way to exclude certain directories from SearchOption using LINQ command like this

string path = "C:\SomeFolder";

var s1 = Directory.GetFiles(path , "*.*", SearchOption.AllDirectories);

var s2 = Directory.GetDirectories(path , "*.*", SearchOption.AllDirectories);

该路径由SUB1和分公司2的文件夹,在它的某些文件。我需要从目录搜索中排除。

The path consists of Sub1 and Sub2 Folders with certain files in it. I need to exclude them from directory search.

感谢

这工作:

string[] exceptions = new string[] { "c:\\SomeFolder\\sub1",
"c:\\SomeFolder\\sub2" };

var s1 = Directory.GetFiles("c:\\x86", "*.*",
SearchOption.AllDirectories).Where(d => exceptions.All(e =>
!d.StartsWith(e)));

<一个href="http://stackoverflow.com/questions/8240861/compare-value-to-array-of-strings-using-startswith">This帮助有例外

推荐答案

没有没有,据我所知。但是你可以用很简单的LINQ做,在一个单行。

No there isn't as far as I know. But you could use very simple LINQ to do that in a single line.

var s1 = Directory.GetFiles(path , "*.*", SearchOption.AllDirectories).Where(d => !d.StartsWith("<EXCLUDE_DIR_PATH>")).ToArray();

您可以轻松地将多个排除目录了。

You can easily combine multiple exclude DIRs too.

这篇关于C#中搜索文件和文件夹,除非在特定的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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