/ Published in: C#
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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. // 2.Store a stack of our directories. // 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. //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; } } } }