【问题标题】:Fastest and simplest way to output Folders Recursion as tree将文件夹递归输出为树的最快和最简单的方法
【发布时间】:2015-11-19 22:23:27
【问题描述】:

有没有更好的方法(更快)来做同样的事情?如果有很多文件夹。
我对算法有点了解,希望有人可以给我一个更好的算法。

我使用以下代码完成工作:

        private static void ShowAllFoldersUnder(string path, int indent)
        {
            try
            {
                if ((File.GetAttributes(path) & FileAttributes.ReparsePoint)
                    != FileAttributes.ReparsePoint)
                {
                    foreach (string folder in Directory.GetDirectories(path))
                    {
                        Console.WriteLine(
                            "{0}{1}", new string(' ', indent), Path.GetFileName(folder));
                        ShowAllFoldersUnder(folder, indent + 2);
                    }
                }
            }
            catch (UnauthorizedAccessException ex) {
                Console.WriteLine(ex.Message); 
            }    
        }

输出样本结果

CompositeUI
  BuilderStrategies
  Collections
  Commands
  Configuration
    Xsd
  EventBroker
  Instrumentation
  obj
    Debug
      TempPE
  Properties
  Services
  SmartParts
  UIElements
  Utility
  Visualizer

【问题讨论】:

  • 多次使用此代码。这是完美的。

标签: c# recursion directory


【解决方案1】:

EnumerateDirectories 可能更快,因为它不必像 GetDirectories 那样分配文件夹名称数组。

【讨论】:

    猜你喜欢
    • 2016-07-13
    • 2017-11-12
    • 1970-01-01
    • 2011-03-03
    • 2010-12-17
    • 2011-01-11
    • 2015-12-21
    • 2011-06-21
    • 1970-01-01
    相关资源
    最近更新 更多