From c2233d83857c5666e0f5db548539a727ec70a729 Mon Sep 17 00:00:00 2001 From: MakesYT <42534870+MakesYT@users.noreply.github.com> Date: Mon, 27 May 2024 00:22:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=BC=80=E6=9C=BA?= =?UTF-8?q?=E8=87=AA=E5=90=AF=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core.Window/AutoStartService.cs | 73 +++++++++++++++++++++ Core/SDKs/Services/Config/ConfigManger.cs | 48 +------------- Core/SDKs/Services/IAutoStartService.cs | 6 ++ KitopiaAvalonia/App.axaml.cs | 1 + KitopiaAvalonia/Windows/MainWindow.axaml.cs | 66 +------------------ 5 files changed, 84 insertions(+), 110 deletions(-) create mode 100644 Core.Window/AutoStartService.cs create mode 100644 Core/SDKs/Services/IAutoStartService.cs 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()