Skip to content

Commit

Permalink
修复 开机自启设置
Browse files Browse the repository at this point in the history
  • Loading branch information
MakesYT committed May 26, 2024
1 parent ad658cf commit c2233d8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 110 deletions.
73 changes: 73 additions & 0 deletions Core.Window/AutoStartService.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
48 changes: 2 additions & 46 deletions Core/SDKs/Services/Config/ConfigManger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Core.ViewModel;
using log4net;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using PluginCore;
using PluginCore.Attribute;
using PluginCore.Config;
Expand Down Expand Up @@ -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<IAutoStartService>()
.ChangeAutoStart(args.Value as bool? ?? false);
break;
}
Expand Down
6 changes: 6 additions & 0 deletions Core/SDKs/Services/IAutoStartService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Core.SDKs.Services;

public interface IAutoStartService
{
public bool ChangeAutoStart(bool autoStart);
}
1 change: 1 addition & 0 deletions KitopiaAvalonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private static IServiceProvider ConfigureServices()
services.AddSingleton<ISearchItemTool, SearchItemTool>();
services.AddTransient<IClipboardService, ClipboardWindow>();
services.AddTransient<IWindowTool, WindowToolServiceWindow>();
services.AddTransient<IAutoStartService, AutoStartService>();
#endif

#if LINUX
Expand Down
66 changes: 2 additions & 64 deletions KitopiaAvalonia/Windows/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<IContentDialog>()
.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<IAutoStartService>()
.ChangeAutoStart(true);
}

private void SetAutoStartupLinux()
Expand Down

0 comments on commit c2233d8

Please sign in to comment.