ex-21-01

来自「Programming Csharp Source Code(代码) Prog」· 代码 · 共 63 行

TXT
63
字号
// Example 21-01: Recursing through subdirectories

namespace Programming_CSharp
{
   using System;
   using System.IO;

   class Tester
   {
      public static void Main()
      {
         Tester t = new Tester();

         // choose the initial subdirectory
         string theDirectory = @"c:\WinNT";

         // call the method to explore the directory,
         // displaying its access date and all
         // subdirectories
         DirectoryInfo dir = new DirectoryInfo(theDirectory);

         t.ExploreDirectory(dir);

         // completed. print the statistics
         Console.WriteLine(
            "\n\n{0} directories found.\n",
            dirCounter);
      }
        
      // Set it running with a directoryInfo object
      // for each directory it finds, it will call 
      // itself recursively
      private void ExploreDirectory(DirectoryInfo dir)
      {
         indentLevel++;  // push a directory level

         // create indentation for subdirectories
         for (int i = 0; i < indentLevel; i++)
            Console.Write("  "); // two spaces per level

         // print the directory and the time last accessed
         Console.WriteLine("[{0}] {1} [{2}]\n", 
            indentLevel, dir.Name, dir.LastAccessTime);

         // get all the directories in the current directory
         // and call this method recursively on each
         DirectoryInfo[] directories = dir.GetDirectories();
         foreach (DirectoryInfo newDir in directories)
         {
            dirCounter++;  // increment the counter
            ExploreDirectory(newDir);
         }
         indentLevel--; // pop a directory level
      }

      // static member variables to keep track of totals
      // and indentation level
      static int dirCounter = 1;
      static int indentLevel  = -1; // so first push = 0
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?