Count Directory recursively in CSharp

Description

The following code shows how to count Directory recursively.

Example


  /*w w  w  . j  ava 2s .  c  om*/
  
  
        
using System;
using System.IO;
public class DirectoryCounter {
    public static void Main() {
        DirectoryInfo myDir = new DirectoryInfo(@"c:\Program Files");
        int total = CountDirectories(myDir);
        Console.WriteLine(total);
    }

    public static int CountDirectories(DirectoryInfo dir) {
        DirectoryInfo[] dirs = dir.GetDirectories();
        int count = dirs.Length;
        foreach (DirectoryInfo subDir in dirs) {
            count += CountDirectories(subDir);
        }
        return count;
    }
}




















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip