优化代码

This commit is contained in:
2025-09-13 21:10:36 +08:00
parent 8c07e9b8a3
commit 326a9f1d75
7 changed files with 39 additions and 12 deletions
+3 -3
View File
@@ -431,7 +431,7 @@ namespace Ink_Canvas
try
{
// 优先从 Settings.json 直接读取
var settingsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Settings.json");
var settingsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs", "Settings.json");
if (File.Exists(settingsPath))
{
var json = File.ReadAllText(settingsPath);
@@ -829,7 +829,7 @@ namespace Ink_Canvas
}
// 备份路径更改为软件根目录下的saves/RegistryBackups文件夹
string backupPath = Path.Combine(RootPath, "saves", "RegistryBackups");
string backupPath = Path.Combine(RootPath, "Saves", "RegistryBackups");
LogHelper.WriteLogToFile($"备份路径: {backupPath}");
if (!Directory.Exists(backupPath))
@@ -1343,7 +1343,7 @@ namespace Ink_Canvas
try
{
// 准备备份目录
string backupPath = Path.Combine(RootPath, "saves", "RegistryBackups");
string backupPath = Path.Combine(RootPath, "Saves", "RegistryBackups");
if (!Directory.Exists(backupPath))
{
Directory.CreateDirectory(backupPath);
+1 -1
View File
@@ -17,7 +17,7 @@ namespace Ink_Canvas.Helpers
// 文件路径策略
private static readonly string DeviceIdFilePath = Path.Combine(App.RootPath, "device_id.dat");
private static readonly string UsageStatsFilePath = Path.Combine(App.RootPath, "usage_stats.enc");
private static readonly string UsageStatsBackupPath = Path.Combine(App.RootPath, "saves", "usage_stats_backup.enc");
private static readonly string UsageStatsBackupPath = Path.Combine(App.RootPath, "Saves", "usage_stats_backup.enc");
private static readonly string DeviceId;
private static readonly object fileLock = new object();
+24 -1
View File
@@ -21,7 +21,7 @@ namespace Ink_Canvas.Helpers
private bool _hotkeysShouldBeRegistered = true; // 启动时注册热键
// 配置文件路径
private static readonly string HotkeyConfigFile = Path.Combine(App.RootPath, "HotkeyConfig.json");
private static readonly string HotkeyConfigFile = Path.Combine(App.RootPath, "Configs", "HotkeyConfig.json");
#endregion
#region Constructor
@@ -30,6 +30,9 @@ namespace Ink_Canvas.Helpers
_mainWindow = mainWindow ?? throw new ArgumentNullException(nameof(mainWindow));
_registeredHotkeys = new Dictionary<string, HotkeyInfo>();
_hotkeysShouldBeRegistered = true; // 启动时注册热键
// 启动时确保配置文件存在
EnsureConfigFileExists();
}
#endregion
@@ -500,6 +503,26 @@ namespace Ink_Canvas.Helpers
}
}
/// <summary>
/// 确保配置文件存在,如果不存在则创建
/// </summary>
private void EnsureConfigFileExists()
{
try
{
// 如果配置文件不存在,创建默认配置文件
if (!File.Exists(HotkeyConfigFile))
{
LogHelper.WriteLogToFile($"快捷键配置文件不存在,创建默认配置文件: {HotkeyConfigFile}", LogHelper.LogType.Event);
CreateDefaultConfigFile();
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"确保快捷键配置文件存在时出错: {ex.Message}", LogHelper.LogType.Error);
}
}
/// <summary>
/// 创建默认的快捷键配置文件
/// </summary>
+2 -4
View File
@@ -20,8 +20,8 @@ namespace Ink_Canvas.Helpers.Plugins
public class PluginManager
{
private static readonly string PluginsDirectory = Path.Combine(App.RootPath, "Plugins");
private static readonly string PluginConfigFile = Path.Combine(App.RootPath, "PluginConfig.json");
private static readonly string PluginConfigBackupFile = Path.Combine(App.RootPath, "PluginConfig.json.bak");
private static readonly string PluginConfigFile = Path.Combine(App.RootPath, "Configs", "PluginConfig.json");
private static readonly string PluginConfigBackupFile = Path.Combine(App.RootPath, "Configs", "PluginConfig.json.bak");
private static PluginManager _instance;
private static SemaphoreSlim _configLock = new SemaphoreSlim(1, 1);
@@ -79,8 +79,6 @@ namespace Ink_Canvas.Helpers.Plugins
Directory.CreateDirectory(PluginsDirectory);
}
// 加载插件配置
LoadConfig();
// 初始化自动保存计时器(3秒)
_autoSaveTimer = new Timer(3000);
+2 -2
View File
@@ -383,7 +383,7 @@ namespace Ink_Canvas
#region Definations and Loading
public static Settings Settings = new Settings();
public static string settingsFileName = "Settings.json";
public static string settingsFileName = Path.Combine("Configs", "Settings.json");
private bool isLoaded;
private bool forcePointEraser;
@@ -417,7 +417,7 @@ namespace Ink_Canvas
}
if (needFix)
{
string newPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "saves");
string newPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Saves");
Settings.Automation.AutoSavedStrokesLocation = newPath;
if (!Directory.Exists(newPath))
Directory.CreateDirectory(newPath);
+6
View File
@@ -2762,6 +2762,12 @@ namespace Ink_Canvas
var text = JsonConvert.SerializeObject(Settings, Formatting.Indented);
try
{
string configsDir = Path.Combine(App.RootPath, "Configs");
if (!Directory.Exists(configsDir))
{
Directory.CreateDirectory(configsDir);
}
File.WriteAllText(App.RootPath + settingsFileName, text);
}
catch { }
+1 -1
View File
@@ -440,7 +440,7 @@ namespace Ink_Canvas
public int MinimumAutomationStrokeNumber { get; set; }
[JsonProperty("autoSavedStrokesLocation")]
public string AutoSavedStrokesLocation = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "saves");
public string AutoSavedStrokesLocation = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Saves");
[JsonProperty("autoDelSavedFiles")]
public bool AutoDelSavedFiles;