Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>Utility class to list files from specified directory</summary>
- class FileListUtil
- {
- public List<FileInfo> Files = new List<FileInfo>(); // List that will hold the files and subfiles in path
- public List<DirectoryInfo> Folders;
- public int FileCount { get; set; }
- public int Start { get; set; }
- public int End { get; set; }
- public FileListUtil(int start, int end)
- {
- Start = start;
- End = end;
- }
- public void FullDirList(DirectoryInfo dir, string searchPattern)
- {
- if (End > 0 && FileCount > End)
- return;
- // list the files
- try
- {
- foreach (FileInfo f in dir.EnumerateFiles(searchPattern))
- {
- if (Start > 0 || End > 0)
- {
- FileCount++;
- if (FileCount < Start)
- continue;
- if (FileCount > End)
- break;
- }
- Files.Add(f);
- }
- }
- catch
- {
- Console.WriteLine("Directory {0} \n could not be accessed!!!!", dir.FullName);
- return; // We alredy got an error trying to access dir so dont try to access it again
- }
- // process each directory
- // If I have been able to see the files in the directory I should also be able
- // to look at its directories so I dont think I should place this in a try catch block
- foreach (DirectoryInfo d in dir.EnumerateDirectories())
- {
- if (Folders != null)
- Folders.Add(d);
- FullDirList(d, searchPattern);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment