Getting a directory tree recursively

using System;
using System.Collections;
using System.IO;
 
 
namespace Devintelligence.IO 
{ 
    public class IOUtils 
    { 
        private IOUtils() 
        { 
        } 
        public static string[] GetAllDirectories(string root) 
        { 
            ArrayList list = new ArrayList(); 
            Stack stack = new Stack(); 
            string[] folders = Directory.GetDirectories(root); 
            AddRange(folders, stack); 
            while (stack.Count > 0) 
            { 
                string path = (string)stack.Pop(); 
                list.Add(path); 
                folders = Directory.GetDirectories(path); 
                AddRange(folders, stack); 
            } 
            return (string[])list.ToArray(typeof(string)); 
        } 
        private static void AddRange(string[] elements, Stack stack) 
        { 
            foreach (string element in elements) 
            { 
                stack.Push(element); 
            } 
        } 
    } 
}


 
 
 

Leave a Reply