From b30d129a1184cb40a3c71e2d1e52d5c4f2ee6bf0 Mon Sep 17 00:00:00 2001 From: uye <99072975+ABA2396@users.noreply.github.com> Date: Sun, 24 Sep 2023 13:44:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BC=80=E6=9C=BA=E8=87=AA=E5=90=AF?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5/=E5=A4=9A=E5=AE=9E=E4=BE=8B=E8=87=AA?= =?UTF-8?q?=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix #6474 fix #5721 fix #6291 --- src/MaaWpfGui/Utilities/AutoStart.cs | 32 ++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/MaaWpfGui/Utilities/AutoStart.cs b/src/MaaWpfGui/Utilities/AutoStart.cs index e10d6d34f6..bab40dfa5f 100644 --- a/src/MaaWpfGui/Utilities/AutoStart.cs +++ b/src/MaaWpfGui/Utilities/AutoStart.cs @@ -11,8 +11,9 @@ // but WITHOUT ANY WARRANTY // +using System; using System.Diagnostics; -using Microsoft.Win32; +using System.IO; namespace MaaWpfGui.Utilities { @@ -21,7 +22,17 @@ namespace MaaWpfGui.Utilities /// public static class AutoStart { - private static readonly string fileValue = Process.GetCurrentProcess().MainModule?.FileName; + private static readonly string _fileValue = Process.GetCurrentProcess().MainModule?.FileName; + private static readonly string _uniqueIdentifier = GetUniqueIdentifierFromPath(_fileValue); + + private static readonly string _startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); + private static readonly string _startupShortcutPath = Path.Combine(_startupFolderPath, $"MAA_{_uniqueIdentifier}.lnk"); + + static string GetUniqueIdentifierFromPath(string path) + { + int hash = path.GetHashCode(); + return hash.ToString("X"); + } /// /// Checks whether this program starts up with OS. @@ -31,8 +42,7 @@ namespace MaaWpfGui.Utilities { try { - using var key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", false); - return key.GetValue("MeoAsst") != null; + return File.Exists(_startupShortcutPath); } catch { @@ -49,14 +59,22 @@ namespace MaaWpfGui.Utilities { try { - using var key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (set) { - key.SetValue("MeoAsst", "\"" + fileValue + "\""); + // 创建启动文件夹的快捷方式 + var shell = new IWshRuntimeLibrary.WshShell(); + var shortcut = shell.CreateShortcut(_startupShortcutPath); + shortcut.TargetPath = _fileValue; + shortcut.WorkingDirectory = Path.GetDirectoryName(_fileValue); + shortcut.Save(); } else { - key.DeleteValue("MeoAsst"); + // 删除启动文件夹的快捷方式 + if (File.Exists(_startupShortcutPath)) + { + File.Delete(_startupShortcutPath); + } } return set == CheckStart();