From 966b2db3123a64acd36a6e4f788cb28634acabc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Thu, 18 Jun 2026 21:43:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=81=E7=A7=BB=E9=83=A8=E5=88=86=E5=B8=B8?= =?UTF-8?q?=E7=94=A8=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shared/General/Logger.cs | 2 +- Shared/Main.cs | 4 +++- Shared/Utils/DirectoryUtils.cs | 2 +- Shared/Utils/PathUtils.cs | 32 +++++++++++++++++++++++--------- Tests/TestUtils.cs | 4 ++-- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/Shared/General/Logger.cs b/Shared/General/Logger.cs index 4530657..2303b76 100644 --- a/Shared/General/Logger.cs +++ b/Shared/General/Logger.cs @@ -291,7 +291,7 @@ public class BaseLogger { /// public class FileLogger : BaseLogger, IDisposable { - public string logFolder = Path.Combine(PathUtils.CurrentFolder, "logs"); + public string logFolder = Path.Combine(Paths.Base, "logs"); public string fileNamePrefix = "Log"; public string fileNameSuffix = ".txt"; private StreamWriter? writer; diff --git a/Shared/Main.cs b/Shared/Main.cs index 464ed93..4286d55 100644 --- a/Shared/Main.cs +++ b/Shared/Main.cs @@ -1,7 +1,9 @@ namespace MeloongCore; public static class Main { - public static void Init(BaseLogger? logger = null) { + public static string AppName = "Meloong"; + public static void Init(string appName, BaseLogger? logger = null) { + AppName = appName; AppDomain.CurrentDomain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(5)); if (logger != null) Logger.Instance = logger; } diff --git a/Shared/Utils/DirectoryUtils.cs b/Shared/Utils/DirectoryUtils.cs index b1111d9..8c80e6a 100644 --- a/Shared/Utils/DirectoryUtils.cs +++ b/Shared/Utils/DirectoryUtils.cs @@ -173,7 +173,7 @@ public static class DirectoryUtils { throw new UnauthorizedAccessException($"不应操作文件夹:{folder}"); } private static readonly Lazy> criticalFolders = new(() => new(new[] { - PathUtils.CurrentFolder, + Paths.Base, Environment.GetFolderPath(Environment.SpecialFolder.Desktop), Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), diff --git a/Shared/Utils/PathUtils.cs b/Shared/Utils/PathUtils.cs index e4296dc..02b362e 100644 --- a/Shared/Utils/PathUtils.cs +++ b/Shared/Utils/PathUtils.cs @@ -173,13 +173,27 @@ public static class PathUtils { #endregion - #region 常用路径 - - /// - /// 程序可执行文件的所在文件夹,以 \ 结尾。 - /// - public static string CurrentFolder => PathUtils.AddSlashSuffix(AppDomain.CurrentDomain.BaseDirectory); - - #endregion - } + +/// +/// 常用路径合集。 +/// 所有路径均以分隔符结尾。 +/// +public static class Paths { + /// + /// 可执行文件所在的文件夹。 + /// + public static string Base => PathUtils.AddSlashSuffix(AppContext.BaseDirectory); + /// + /// 可执行文件所在的文件夹中,名为 的子文件夹。 + /// + public static string BaseThenName => PathUtils.AddSlashSuffix(Base + Main.AppName); + /// + /// %AppData% 文件夹。 + /// + public static string AppData => PathUtils.AddSlashSuffix(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); + /// + /// %AppData% 文件夹中,名为 的子文件夹。 + /// + public static string AppDataThenName => PathUtils.AddSlashSuffix(AppData + Main.AppName); +} \ No newline at end of file diff --git a/Tests/TestUtils.cs b/Tests/TestUtils.cs index b204941..833e1d8 100644 --- a/Tests/TestUtils.cs +++ b/Tests/TestUtils.cs @@ -7,7 +7,7 @@ public abstract class TestBase { public TestBase() { - MeloongCore.Main.Init(new TestLogger()); + MeloongCore.Main.Init("MeloongCoreTest", new TestLogger()); } private class TestLogger : BaseLogger { public override void HandleBehavior(string? rawMessage, string formattedMessage, LogBehavior behavior, Exception? ex) @@ -40,7 +40,7 @@ public abstract class TestWithFolder : TestBase { /// 输出指定的测试用文件,返回文件路径。 /// public string GetTestFile(string fileName) { - var sourceFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", GetType().Name.Replace("Test", ""), fileName); + var sourceFilePath = Path.Combine(Paths.Base, "TestFiles", GetType().Name.Replace("Test", ""), fileName); var distPath = Path.Combine(tempFolder, PathUtils.GetLastPart(sourceFilePath)); FileUtils.Copy(sourceFilePath, distPath); return distPath;