Skip to content

Commit

Permalink
Fix: Startup Run
Browse files Browse the repository at this point in the history
  • Loading branch information
GST-Main committed Mar 25, 2023
1 parent 1e637ab commit d0f7f78
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -113,17 +116,17 @@ 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()
{
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;
}
Expand All @@ -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);
}
}

Expand Down

0 comments on commit d0f7f78

Please sign in to comment.