C#搜索某些文件夹以外的文件和文件夹 [英] C# Searching for files and folders except in certain folders

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

问题描述

有没有办法使用LINQ命令从SearchOption中排除某些目录像这样

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和Sub2文件夹组成,其中包含某些文件。我需要从目录搜索中排除它们。

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

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();

您也可以轻松地组合多个排除DIR。

You can easily combine multiple exclude DIRs too.

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

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