Check that any directory exists inside given one

廉价感情. 提交于 2020-01-07 02:19:05

问题


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)

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.

Is there a way in .NET to determine 1 subdirectory quickly?


回答1:


if (Directory.EnumerateDirectories().Any())

EnumerateDirectories will return directories only as you enumerate the returned sequence (deferred execution).




回答2:


Use the function overload this will only look for directories in the current directory, not the entire tree:

if (Directory.GetDirectories(path, "*.*", SearchOption.TopDirectoryOnly).Length != 0)


来源:https://stackoverflow.com/questions/9504073/check-that-any-directory-exists-inside-given-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!