Home   Cover Cover Cover Cover
 

Directories and files

Chap4_Ex5.cs
using System;
using System.IO;

public class DirectoryWalker {

  public static void TreeDump(string path, string tabs) {
    if(!Directory.Exists(path)) Console.WriteLine("Directory does not exist!");
    else {
      string[] fs = Directory.GetFiles(path);
      foreach(string s in fs) {
        FileInfo fi = new FileInfo(s);
        Console.WriteLine(tabs+"\t"+fi.Name);
      }
      string[] ds = Directory.GetDirectories(path);
      foreach(string d in ds) {
        DirectoryInfo di = new DirectoryInfo(d);
        Console.WriteLine(tabs+"\t"+di.Name);
        TreeDump(di.FullName,tabs+"\t");
      }
    }
  }

  public static void Main(string[] args) {
    Console.Write("directory?:"); 
    TreeDump(Console.ReadLine(),"");
  }

}