检查给定目录中是否存在任何目录

编程入门 行业动态 更新时间:2024-10-27 20:33:51
本文介绍了检查给定目录中是否存在任何目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的应用程序是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).

更多推荐

检查给定目录中是否存在任何目录

本文发布于:2023-11-27 15:43:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638612.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是否存在   目录   目录中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!