From 5b9a6d46333206321e692ad9e35ca054b997d841 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 25 Nov 2021 10:35:35 +0000 Subject: [PATCH] Updated accessible-directory --- 2021/11/AccessibleDirectory.cs | 67 ++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 8 deletions(-) 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)