本文介绍了检查给定目录中是否存在任何目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序是Windows,C#3.0.我要确保给定的目录不包含子目录.天真的代码,例如
My application is Windows, C# 3.0. I want to make sure that a directory given does not contains subdirectories. Naive code like
if (Directory.GetDirectories(path).Length != 0)在包含以下内容的目录上将非常缓慢地工作10000个子目录,因为它将建立一个子目录列表,而即使只有1个目录对我来说已经足够.
will work very slow on directories which contain e.g. 10000 subdirectories, because it will build a list of subdirectories, while even 1 directory is already enough for me.
.NET中是否可以快速确定1个子目录?
Is there a way in .NET to determine 1 subdirectory quickly?
推荐答案if (Directory.EnumerateDirectories().Any())
EnumerateDirectories仅在枚举返回的序列(延迟执行)时才返回目录.
EnumerateDirectories will return directories only as you enumerate the returned sequence (deferred execution).
更多推荐
检查给定目录中是否存在任何目录
发布评论