当下一个目录在列表中时停止GetDirectories。 [英] Stop GetDirectories when the next directory is in a list.

查看:115
本文介绍了当下一个目录在列表中时停止GetDirectories。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方法让用户选择一些文件夹或驱动器在文件搜索中排除,并且不要扫描子文件夹。我使用树视图来选择排除的路径并将它们存储在名为SkipFolders的列表中(List< String> SkipFolders
= new List< string>();)。然后我需要其余的文件夹并使用下面的代码。我用简单的英文写下我想要的下划线文字。

I have have been searching for a way to let a user select some folders or drives to exclude in a file search, and dont scan the subfolders. I use a treeview to select the exluded paths and store them in a list called SkipFolders (List<String> SkipFolders = new List<string>();). Then I need the rest of the folders and use the code below. Ill write in plain english what I want in underlined text.

推荐答案

使用递归函数有时会增加内存使用量。为什么不使用

DirectoryInfo [] GetDirectories()方法
,附带参数SearchOption,允许您在搜索中包含所有子目录。然后你可以做类似的事情:

Using a recursive function comes sometimes with an increased memory usage. Why don't you use the DirectoryInfo[] GetDirectories() method, that comes with a parameter SearchOption allowing you to include all subdirectories in your search. Then you could do something like:

List<string> skippedFolders = new List<string>();   // fill this List with skipped dirs.
DirectoryInfo di = new DirectoryInfo(path);
DirectoryInfo[] directories = di.GetDirectories("*", SearchOption.AllDirectories);
var result = directories.Where(x => !skippedFolders.Contains(x.FullName));

其中'skippedFolders'可以是List< string>使用跳过的dirs的全名。

where 'skippedFolders' could be a List<string> with the fullnames of your skipped dirs.

wizend


这篇关于当下一个目录在列表中时停止GetDirectories。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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