-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
63 lines (54 loc) · 1.68 KB
/
Copy pathProgram.cs
File metadata and controls
63 lines (54 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Windows.Forms;
using Microsoft.Win32;
namespace TowerTapes;
static class Program
{
private static Mutex? _mutex;
private const string StartupKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
private const string AppName = "TowerTapes";
[STAThread]
static void Main()
{
_mutex = new Mutex(true, @"Global\TowerTapes_SingleInstance", out bool created);
if (!created)
{
MessageBox.Show("TowerTapes is already running.", "TowerTapes",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
// Apply startup setting on launch
var config = Config.Load();
SetStartupEnabled(config.LaunchAtStartup);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
GC.KeepAlive(_mutex);
}
public static void SetStartupEnabled(bool enabled)
{
try
{
using var key = Registry.CurrentUser.OpenSubKey(StartupKey, writable: true);
if (key == null) return;
if (enabled)
{
var exePath = Environment.ProcessPath ?? Application.ExecutablePath;
key.SetValue(AppName, $"\"{exePath}\"");
}
else
{
key.DeleteValue(AppName, throwOnMissingValue: false);
}
}
catch { }
}
public static bool IsStartupEnabled()
{
try
{
using var key = Registry.CurrentUser.OpenSubKey(StartupKey);
return key?.GetValue(AppName) != null;
}
catch { return false; }
}
}