diff --git a/Core.Window/AutoStartService.cs b/Core.Window/AutoStartService.cs new file mode 100644 index 0000000..9b3d8d1 --- /dev/null +++ b/Core.Window/AutoStartService.cs @@ -0,0 +1,73 @@ +using System.IO; +using Core.SDKs.Services; +using log4net; +using Microsoft.Win32; +using PluginCore; + +namespace Core.Window; + +public class AutoStartService : IAutoStartService +{ + private static readonly ILog log = LogManager.GetLogger(nameof(AutoStartService)); + + public bool ChangeAutoStart(bool autoStart) + { + if (autoStart) + { + var strName = AppDomain.CurrentDomain.BaseDirectory + "KitopiaAvalonia.exe"; //获取要自动运行的应用程序名 + if (!File.Exists(strName)) //判断要自动运行的应用程序文件是否存在 + { + return false; + } + + var registry = + Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", + true); //检索指定的子项 + if (registry == null) //若指定的子项不存在 + { + registry = Registry.CurrentUser.CreateSubKey( + "Software\\Microsoft\\Windows\\CurrentVersion\\Run"); //则创建指定的子项 + } + else + { + if (Equals(registry.GetValue("Kitopia"), $"\"{strName}\"")) + { + log.Info("开机自启配置已存在"); + return true; + } + } + + log.Info("用户确认启用开机自启"); + try + { + registry.SetValue("Kitopia", $"\"{strName}\""); //设置该子项的新的“键值对” + ((IToastService)ServiceManager.Services.GetService(typeof(IToastService))).Show("开机自启", + "开机自启设置成功"); + } + catch (Exception exception) + { + log.Error("开机自启设置失败"); + log.Error(exception.StackTrace); + ((IToastService)ServiceManager.Services.GetService(typeof(IToastService))).Show("开机自启", + "开机自启设置失败"); + return false; + } + } + else + { + try + { + var registry = + Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", + true); //检索指定的子项 + registry?.DeleteValue("Kitopia"); + } + catch (Exception) + { + return false; + } + } + + return true; + } +} \ No newline at end of file diff --git a/Core/SDKs/Services/Config/ConfigManger.cs b/Core/SDKs/Services/Config/ConfigManger.cs index 5b648e9..13d788e 100644 --- a/Core/SDKs/Services/Config/ConfigManger.cs +++ b/Core/SDKs/Services/Config/ConfigManger.cs @@ -8,7 +8,6 @@ using Core.ViewModel; using log4net; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Win32; using PluginCore; using PluginCore.Attribute; using PluginCore.Config; @@ -79,51 +78,8 @@ public static void Init() { case "autoStart": { - if ((bool)args.Value) - { - var strName = AppDomain.CurrentDomain.BaseDirectory + "Kitopia.exe"; //获取要自动运行的应用程序名 - if (!File.Exists(strName)) //判断要自动运行的应用程序文件是否存在 - { - return; - } - - var registry = - Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", - true); //检索指定的子项 - if (registry == null) //若指定的子项不存在 - { - registry = Registry.CurrentUser.CreateSubKey( - "Software\\Microsoft\\Windows\\CurrentVersion\\Run"); //则创建指定的子项 - } - - log.Info("用户确认启用开机自启"); - try - { - registry.SetValue("Kitopia", $"\"{strName}\""); //设置该子项的新的“键值对” - ((IToastService)ServiceManager.Services.GetService(typeof(IToastService))).Show("开机自启", - "开机自启设置成功"); - } - catch (Exception exception) - { - log.Error("开机自启设置失败"); - log.Error(exception.StackTrace); - ((IToastService)ServiceManager.Services.GetService(typeof(IToastService))).Show("开机自启", - "开机自启设置失败"); - } - } - else - { - try - { - var registry = - Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", - true); //检索指定的子项 - registry?.DeleteValue("Kitopia"); - } - catch (Exception) - { - } - } + ServiceManager.Services.GetService() + .ChangeAutoStart(args.Value as bool? ?? false); break; } diff --git a/Core/SDKs/Services/IAutoStartService.cs b/Core/SDKs/Services/IAutoStartService.cs new file mode 100644 index 0000000..fd7db7c --- /dev/null +++ b/Core/SDKs/Services/IAutoStartService.cs @@ -0,0 +1,6 @@ +namespace Core.SDKs.Services; + +public interface IAutoStartService +{ + public bool ChangeAutoStart(bool autoStart); +} \ No newline at end of file diff --git a/KitopiaAvalonia/App.axaml.cs b/KitopiaAvalonia/App.axaml.cs index 47237dc..25cf4b1 100644 --- a/KitopiaAvalonia/App.axaml.cs +++ b/KitopiaAvalonia/App.axaml.cs @@ -79,6 +79,7 @@ private static IServiceProvider ConfigureServices() services.AddSingleton(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); #endif #if LINUX diff --git a/KitopiaAvalonia/Windows/MainWindow.axaml.cs b/KitopiaAvalonia/Windows/MainWindow.axaml.cs index b657ff9..bbb10cc 100644 --- a/KitopiaAvalonia/Windows/MainWindow.axaml.cs +++ b/KitopiaAvalonia/Windows/MainWindow.axaml.cs @@ -13,7 +13,6 @@ using Avalonia.Media; using Avalonia.Styling; using Avalonia.Threading; -using Core.SDKs; using Core.SDKs.CustomScenario; using Core.SDKs.Services; using Core.SDKs.Services.Config; @@ -26,8 +25,6 @@ using KitopiaAvalonia.Pages; using log4net; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Win32; -using PluginCore; using HotKeyManager = Core.SDKs.HotKey.HotKeyManager; namespace KitopiaAvalonia; @@ -271,67 +268,8 @@ private void SetAutoStartup() private void SetAutoStartupWindow() { - var strName = AppDomain.CurrentDomain.BaseDirectory + "KitopiaAvalonia.exe"; //获取要自动运行的应用程序名 - if (!File.Exists(strName)) //判断要自动运行的应用程序文件是否存在 - { - return; - } - - var registry = - Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true); //检索指定的子项 - if (registry == null) //若指定的子项不存在 - { - registry = Registry.CurrentUser.CreateSubKey( - "Software\\Microsoft\\Windows\\CurrentVersion\\Run"); //则创建指定的子项 - } - - if (registry.GetValue("Kitopia") is null) - { - ServiceManager.Services.GetService() - .ShowDialogAsync(null, new DialogContent("Kitopia", - new TextBlock() - { - Text = "是否设置开机自启?\n可能被杀毒软件阻止 ", - FontSize = 15 - }, "取消", null, "确定", () => { - log.Info("用户确认启用开机自启"); - try - { - registry.SetValue("Kitopia", $"\"{strName}\""); //设置该子项的新的“键值对” - ((IToastService)ServiceManager.Services.GetService(typeof(IToastService))!).Show( - "开机自启", - "开机自启设置成功"); - } - catch (Exception exception) - { - log.Error("开机自启设置失败"); - log.Error(exception.StackTrace); - ((IToastService)ServiceManager.Services.GetService(typeof(IToastService))!).Show( - "开机自启", - "开机自启设置失败,请检查杀毒软件后重试"); - } - }, null, () => { log.Info("用户取消启用开机自启"); })); - } - else if (!registry.GetValue("Kitopia") - .Equals($"\"{strName}\"")) - { - try - { - registry.SetValue("Kitopia", $"\"{strName}\""); //设置该子项的新的“键值对” - ((IToastService)ServiceManager.Services.GetService(typeof(IToastService))!).Show("开机自启", "开机自启设置成功"); - } - catch (Exception exception) - { - log.Error("开机自启设置失败"); - log.Error(exception.StackTrace); - ((IToastService)ServiceManager.Services.GetService(typeof(IToastService))!).Show("开机自启", - "开机自启设置失败,请检查杀毒软件后重试"); - } - } - else - { - log.Debug("程序自启已存在"); - } + ServiceManager.Services.GetService() + .ChangeAutoStart(true); } private void SetAutoStartupLinux()