-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsStartupHelper.cs
More file actions
114 lines (100 loc) · 4.25 KB
/
WindowsStartupHelper.cs
File metadata and controls
114 lines (100 loc) · 4.25 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Diagnostics;
using Microsoft.Win32;
namespace KemTranslate
{
internal static class WindowsStartupHelper
{
private const string RunKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Run";
private const string StartupApprovedRunKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run";
private const string ValueName = "KemTranslate";
internal static bool IsEnabled()
{
try
{
using var runKey = Registry.CurrentUser.OpenSubKey(RunKeyPath, writable: false);
var command = runKey?.GetValue(ValueName) as string;
if (!string.Equals(GetExecutablePath(command), GetCurrentExecutablePath(), StringComparison.OrdinalIgnoreCase))
return false;
using var startupApprovedKey = Registry.CurrentUser.OpenSubKey(StartupApprovedRunKeyPath, writable: false);
var startupApprovedValue = startupApprovedKey?.GetValue(ValueName) as byte[];
return IsStartupApprovedEnabled(startupApprovedValue);
}
catch (Exception ex)
{
AppLogger.Log(ex, "WindowsStartupHelper.IsEnabled failed");
return false;
}
}
internal static bool TrySetEnabled(bool enabled, out string? errorMessage)
{
try
{
using var runKey = Registry.CurrentUser.OpenSubKey(RunKeyPath, writable: true)
?? Registry.CurrentUser.CreateSubKey(RunKeyPath);
using var startupApprovedKey = Registry.CurrentUser.OpenSubKey(StartupApprovedRunKeyPath, writable: true)
?? Registry.CurrentUser.CreateSubKey(StartupApprovedRunKeyPath);
if (runKey == null)
{
errorMessage = "Unable to open the Windows startup registry key.";
return false;
}
if (startupApprovedKey == null)
{
errorMessage = "Unable to open the Windows startup approval registry key.";
return false;
}
if (enabled)
{
runKey.SetValue(ValueName, $"\"{GetCurrentExecutablePath()}\"");
startupApprovedKey.DeleteValue(ValueName, throwOnMissingValue: false);
}
else
{
runKey.DeleteValue(ValueName, throwOnMissingValue: false);
startupApprovedKey.DeleteValue(ValueName, throwOnMissingValue: false);
}
errorMessage = null;
return true;
}
catch (Exception ex)
{
AppLogger.Log(ex, "WindowsStartupHelper.TrySetEnabled failed");
errorMessage = ex.Message;
return false;
}
}
internal static bool IsStartupApprovedEnabled(byte[]? startupApprovedValue)
{
if (startupApprovedValue == null || startupApprovedValue.Length == 0)
return true;
return startupApprovedValue[0] switch
{
0x02 => true,
0x03 => false,
0x06 => true,
0x07 => false,
_ => true
};
}
private static string GetCurrentExecutablePath()
{
return Environment.ProcessPath
?? Process.GetCurrentProcess().MainModule?.FileName
?? AppContext.BaseDirectory.TrimEnd(System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar);
}
internal static string? GetExecutablePath(string? command)
{
if (string.IsNullOrWhiteSpace(command))
return null;
var trimmed = command.Trim();
if (trimmed.StartsWith('"'))
{
var closingQuoteIndex = trimmed.IndexOf('"', 1);
return closingQuoteIndex > 1 ? trimmed.Substring(1, closingQuoteIndex - 1) : null;
}
var firstSpaceIndex = trimmed.IndexOf(' ');
return firstSpaceIndex > 0 ? trimmed[..firstSpaceIndex] : trimmed;
}
}
}