From d0f7f78815be008aafc71fe4e69b9a6098ca7994 Mon Sep 17 00:00:00 2001 From: GST Date: Sat, 25 Mar 2023 12:24:20 +0900 Subject: [PATCH] Fix: Startup Run --- Form1.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Form1.cs b/Form1.cs index 80b7cf5..f266dc4 100644 --- a/Form1.cs +++ b/Form1.cs @@ -21,6 +21,9 @@ public partial class Form1 : Form private bool isLShiftPressed = false; private bool launchOnStartup; + private string exePath = Application.ExecutablePath; + private string appName = Application.ProductName; + // Send Key [return: MarshalAs(UnmanagedType.Bool)] [DllImport("user32.dll")] @@ -113,9 +116,9 @@ private void SetStartupLaunch(bool launchOnStartup) RegistryKey? rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (launchOnStartup) - rk?.SetValue(Application.ProductName, AppDomain.CurrentDomain.BaseDirectory); + rk?.SetValue(appName, exePath); else - rk?.DeleteValue(Application.ProductName, throwOnMissingValue: false); + rk?.DeleteValue(appName, throwOnMissingValue: false); } private bool GetStartupLaunch() @@ -123,7 +126,7 @@ private bool GetStartupLaunch() RegistryKey? rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (rk != null) { - string? value = (string?)rk.GetValue(Application.ProductName); + string? value = (string?)rk.GetValue(appName); if (value != null) return true; } @@ -136,9 +139,9 @@ private void UpdateStartupLaunchPath() RegistryKey? rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (rk != null) { - string? currentPath = (string?)rk.GetValue(Application.ProductName); - if (currentPath != null && currentPath != AppDomain.CurrentDomain.BaseDirectory) - rk?.SetValue(Application.ProductName, AppDomain.CurrentDomain.BaseDirectory); + string? currentPath = (string?)rk.GetValue(appName); + if (currentPath != null && currentPath != exePath) + rk?.SetValue(appName, exePath); } }