Changed: accessible-directory refactoring

This commit is contained in:
Ray 2021-10-31 20:13:15 +00:00
parent 1d43e19ce2
commit 53ee21acf4
2 changed files with 75 additions and 3 deletions

View File

@ -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());

View File

@ -5,7 +5,7 @@ using System.Linq;
namespace RandomFileRunner.IO
{
public class SmartDirectory
public class AccessibleDirectory
{
public static List<string> GetFiles(string path, string pattern, bool searchTopOnly)
@ -73,6 +73,68 @@ namespace RandomFileRunner.IO
return fileList;
}
public static IEnumerable<string> EnumerateFiles(string path, string pattern, bool searchTopOnly)
{
List<string> directoryList = new List<string>();
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<string> searchDirList = new List<string>();
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<string, ulong, int, bool> callback)
{
List<string> directoryList = new List<string>();
@ -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;