diff --git a/2021/11/AccessibleDirectory.cs b/2021/11/AccessibleDirectory.cs index a4af53d..ae18269 100644 --- a/2021/11/AccessibleDirectory.cs +++ b/2021/11/AccessibleDirectory.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; namespace RyzStudio.IO { @@ -214,16 +215,66 @@ namespace RyzStudio.IO string filename = Path.GetFileName(filepath); if (string.IsNullOrWhiteSpace(filename)) return null; - List fileList = AccessibleDirectory.GetFiles(path, pattern, true); - if (fileList == null) return null; - if (fileList.Count <= 0) return null; - if (fileList.Count(x => (Path.GetFileName(x).Equals(filename))) < 0) return null; + bool returnNext = false; + foreach (string item in Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)) + { + if (!MatchFileSearchPattern(pattern, Path.GetFileName(item))) + { + continue; + } - int pos = fileList.FindIndex(x => Path.GetFileName(x).Equals(filename)); - if (pos < 0) return null; - if (pos >= (fileList.Count - 1)) return null; + if (!IsFileAccessible(item)) + { + continue; + } - return fileList[(pos + 1)]; + if (returnNext) + { + return item; + } + + if (Path.GetFileName(item).Equals(filename)) + { + returnNext = true; + } + } + + return null; + } + + public static string GetPreviousFile(string filepath, string pattern) + { + if (string.IsNullOrWhiteSpace(filepath)) return null; + + string path = Path.GetDirectoryName(filepath); + if (string.IsNullOrWhiteSpace(path)) return null; + + string filename = Path.GetFileName(filepath); + if (string.IsNullOrWhiteSpace(filename)) return null; + + StringBuilder sb = new StringBuilder(); + foreach (string item in Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)) + { + if (!MatchFileSearchPattern(pattern, Path.GetFileName(item))) + { + continue; + } + + if (!IsFileAccessible(item)) + { + continue; + } + + sb.Clear(); + sb.Append(item); + + if (Path.GetFileName(item).Equals(filename)) + { + return (sb.Length <= 0) ? null : sb.ToString(); + } + } + + return null; } public static bool IsAccessible(string path)