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

查看:29
本文介绍了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)));

有助于异常

推荐答案

据我所知没有.但是您可以使用非常简单的 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天全站免登陆