Return to Snippet

Revision: 39982
at January 24, 2011 12:45 by kyrathaba


Initial Code
namespace Kyrathasoft.FilesDirectories.GetFilesRecursively {

using System;
using System.Collections.Generic;
using System.IO;

    public static class clsGetFilesRecursively {

        public static List<string> GetFilesRecursively(string b) {

            // 1.Store results in the file results list.
            List<string> result = new List<string>();

            // 2.Store a stack of our directories.
            Stack<string> stack = new Stack<string>();

            // 3.Add initial directory.
            stack.Push(b);

            // 4.Continue while there are directories to process
            while (stack.Count > 0) {
                // A.Get top directory
                string dir = stack.Pop();

                try {
                    // B. Add all files at this directory to the result List.
                    result.AddRange(Directory.GetFiles(dir, "*.*"));

                    // C. Add all directories at this directory.
                    foreach (string dn in Directory.GetDirectories(dir)) {
                        stack.Push(dn);
                    }
                }
                catch {
                    // D. Could not open the directory
                }
            }
            return result;
        }


        public static int getNumOfSubdirs(string topLvDir) {

            int totalSubdirs = 0;

            //Store a stack of our directories.
            Stack<string> stack = new Stack<string>();

            //Add initial directory.
            stack.Push(topLvDir);

            //Continue while there are directories to process
            while (stack.Count > 0) {
                //Get top directory
                string dir = stack.Pop();

                try {
                    //Add all directories at this directory.
                    foreach (string dn in Directory.GetDirectories(dir)) {
                        totalSubdirs++;            
                    }
                }
                catch {
                    // D. Could not open the directory
                }
            }
            return totalSubdirs;

        }

    }



    public class encappedListOfStrings {
        public encappedListOfStrings() { }
        public encappedListOfStrings(List<string> the_list) {
            _listOfStrings = the_list;
        }
        private List<string> _listOfStrings;
        public List<string> ListOfStrings {
            get { return _listOfStrings; }
        }
    }


}

Initial URL

                                

Initial Description

                                

Initial Title
get files recursively

Initial Tags
files, directory

Initial Language
C#