andrew4582

FileListUtil - Get Files Utility

Jul 5th, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1.         /// <summary>Utility class to list files from specified directory</summary>
  2.         class FileListUtil
  3.         {
  4.             public List<FileInfo> Files = new List<FileInfo>(); // List that will hold the files and subfiles in path
  5.             public List<DirectoryInfo> Folders;
  6.             public int FileCount { get; set; }
  7.             public int Start { get; set; }
  8.             public int End { get; set; }
  9.  
  10.             public FileListUtil(int start, int end)
  11.             {
  12.                 Start = start;
  13.                 End = end;
  14.             }
  15.  
  16.             public void FullDirList(DirectoryInfo dir, string searchPattern)
  17.             {
  18.                 if (End > 0 && FileCount > End)
  19.                     return;
  20.  
  21.                 // list the files
  22.                 try
  23.                 {
  24.                     foreach (FileInfo f in dir.EnumerateFiles(searchPattern))
  25.                     {
  26.                         if (Start > 0 || End > 0)
  27.                         {
  28.                             FileCount++;
  29.                             if (FileCount < Start)
  30.                                 continue;
  31.                             if (FileCount > End)
  32.                                 break;
  33.                         }
  34.                         Files.Add(f);
  35.                     }
  36.  
  37.                 }
  38.                 catch
  39.                 {
  40.                     Console.WriteLine("Directory {0}  \n could not be accessed!!!!", dir.FullName);
  41.                     return; // We alredy got an error trying to access dir so dont try to access it again
  42.                 }
  43.  
  44.                 // process each directory
  45.                 // If I have been able to see the files in the directory I should also be able
  46.                 // to look at its directories so I dont think I should place this in a try catch block
  47.                 foreach (DirectoryInfo d in dir.EnumerateDirectories())
  48.                 {
  49.                     if (Folders != null)
  50.                         Folders.Add(d);
  51.                     FullDirList(d, searchPattern);
  52.                 }
  53.             }
  54.         }
Advertisement
Add Comment
Please, Sign In to add comment