diff --git a/MainForm.cs b/MainForm.cs index 611ac3b..ed7b7e3 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -289,7 +289,7 @@ namespace RandomFileRunner if (File.Exists(item)) { - if (SmartDirectory.IsFileAccessible(item)) + if (AccessibleDirectory.IsFileAccessible(item)) { foundFiles.Add(item); ThreadControl.SetText(label2, foundFiles.Count.ToString()); @@ -300,7 +300,7 @@ namespace RandomFileRunner if (Directory.Exists(item)) { - SmartDirectory.GetFiles(item, textBox1.Text, this.CurrentSession.SearchTopDirectoryOnly, SearchDirecory_OnFound); + AccessibleDirectory.GetFiles(item, textBox1.Text, this.CurrentSession.SearchTopDirectoryOnly, SearchDirecory_OnFound); ThreadControl.SetText(label2, foundFiles.Count.ToString()); diff --git a/RyzStudio/IO/SmartDirectory.cs b/RyzStudio/IO/AccessibleDirectory.cs similarity index 79% rename from RyzStudio/IO/SmartDirectory.cs rename to RyzStudio/IO/AccessibleDirectory.cs index 11ef2af..44e9472 100644 --- a/RyzStudio/IO/SmartDirectory.cs +++ b/RyzStudio/IO/AccessibleDirectory.cs @@ -5,7 +5,7 @@ using System.Linq; namespace RandomFileRunner.IO { - public class SmartDirectory + public class AccessibleDirectory { public static List GetFiles(string path, string pattern, bool searchTopOnly) @@ -73,6 +73,68 @@ namespace RandomFileRunner.IO return fileList; } + public static IEnumerable EnumerateFiles(string path, string pattern, bool searchTopOnly) + { + List directoryList = new List(); + + if (string.IsNullOrWhiteSpace(pattern)) pattern = "*"; + + directoryList.Add(path); + + while (true) + { + if (directoryList.Count <= 0) + { + yield break; + } + + string directory = directoryList.First(); + directoryList.RemoveAt(0); + + if (IsDirectoryAccessible(directory)) + { + IEnumerable searchDirList = new List(); + + try + { + searchDirList = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly); + } + catch (Exception) + { + continue; + } + + if (!searchTopOnly) + { + foreach (string item in searchDirList) + { + if (!IsDirectoryAccessible(item)) + { + continue; + } + + directoryList.Add(item); + } + } + + foreach (string item in Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly)) + { + if (!MatchFileSearchPattern(pattern, Path.GetFileName(item))) + { + continue; + } + + if (!IsFileAccessible(item)) + { + continue; + } + + yield return item; + } + } + } + } + public static void GetFiles(string path, string pattern, bool searchTopOnly, Func callback) { List directoryList = new List(); @@ -142,6 +204,16 @@ namespace RandomFileRunner.IO } } + public static bool IsAccessible(string path) + { + if (string.IsNullOrWhiteSpace(path)) return false; + + if (File.Exists(path)) return IsFileAccessible(path); + if (Directory.Exists(path)) return IsDirectoryAccessible(path); + + return false; + } + public static bool IsFileAccessible(string filename) { if (string.IsNullOrWhiteSpace(filename)) return false;