-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartupManager.cs
More file actions
59 lines (54 loc) · 1.66 KB
/
Copy pathStartupManager.cs
File metadata and controls
59 lines (54 loc) · 1.66 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
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;
namespace UiVisualDebugger;
public static class StartupManager
{
private const string RUN_KEY_PATH = @"Software\Microsoft\Windows\CurrentVersion\Run";
private const string APP_NAME = "UiVisualDebugger";
public static bool IsStartupEnabled()
{
try
{
using RegistryKey? key = Registry.CurrentUser.OpenSubKey(RUN_KEY_PATH, false);
string? value = key?.GetValue(APP_NAME) as string;
return !string.IsNullOrEmpty(value);
}
catch (Exception ex)
{
Debug.WriteLine($"[StartupManager] Check error: {ex.Message}");
return false;
}
}
public static bool SetStartup(bool enable)
{
try
{
using RegistryKey? key = Registry.CurrentUser.OpenSubKey(RUN_KEY_PATH, true);
if (key == null) return false;
if (enable)
{
string exePath = Process.GetCurrentProcess().MainModule?.FileName ?? "";
if (string.IsNullOrEmpty(exePath))
{
exePath = Path.Combine(AppContext.BaseDirectory, "UiVisualDebugger.exe");
}
key.SetValue(APP_NAME, $"\"{exePath}\"");
}
else
{
if (key.GetValue(APP_NAME) != null)
{
key.DeleteValue(APP_NAME, false);
}
}
return true;
}
catch (Exception ex)
{
Debug.WriteLine($"[StartupManager] Set error: {ex.Message}");
return false;
}
}
}