Skip to content

Commit

Permalink
Update: Launch On Startup
Browse files Browse the repository at this point in the history
  • Loading branch information
GST-Main committed Mar 25, 2023
1 parent 87bd836 commit 1e637ab
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 38 deletions.
83 changes: 45 additions & 38 deletions Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions Form1.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
Expand All @@ -18,6 +19,7 @@ public partial class Form1 : Form
private const uint WM_KEYUP = 0x2;

private bool isLShiftPressed = false;
private bool launchOnStartup;

// Send Key
[return: MarshalAs(UnmanagedType.Bool)]
Expand All @@ -28,6 +30,10 @@ public Form1()
{
InitializeComponent();
SetupKeyboardHooks();
launchOnStartup = GetStartupLaunch();
toolStripMenuItem_LaunchOnStartup.Checked = launchOnStartup;
if (launchOnStartup)
UpdateStartupLaunchPath();
}

public void SetupKeyboardHooks()
Expand Down Expand Up @@ -101,5 +107,51 @@ private void Form1_Shown(object sender, EventArgs e)
{
Hide();
}

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);
else
rk?.DeleteValue(Application.ProductName, 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);
if (value != null)
return true;
}

return false;
}

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);
}
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void toolStripMenuItem_LaunchOnStartup_Click(object sender, EventArgs e)
{
SetStartupLaunch(!launchOnStartup);
launchOnStartup = GetStartupLaunch();
toolStripMenuItem_LaunchOnStartup.Checked = launchOnStartup;
}
}
}

0 comments on commit 1e637ab

Please sign in to comment.