-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartupHelper.cs
More file actions
44 lines (38 loc) · 1.31 KB
/
Copy pathStartupHelper.cs
File metadata and controls
44 lines (38 loc) · 1.31 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
using System.Diagnostics;
namespace Loadout;
/// <summary>
/// Start-with-Windows toggle. Because Stowbyte runs elevated (requireAdministrator), a plain
/// HKCU\...\Run entry would trigger a UAC prompt at every logon. Instead we register a Scheduled
/// Task that runs at logon "with highest privileges" — it launches silently, already elevated.
/// </summary>
public static class StartupHelper
{
private const string TaskName = "Stowbyte";
private static string ExePath =>
Environment.ProcessPath ?? Process.GetCurrentProcess().MainModule!.FileName!;
public static bool IsEnabled()
{
try { return Run($"/Query /TN \"{TaskName}\"") == 0; }
catch { return false; }
}
public static void SetEnabled(bool on)
{
if (on)
Run($"/Create /TN \"{TaskName}\" /TR \"\\\"{ExePath}\\\"\" /SC ONLOGON /RL HIGHEST /F");
else
Run($"/Delete /TN \"{TaskName}\" /F");
}
private static int Run(string args)
{
var psi = new ProcessStartInfo("schtasks.exe", args)
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using var p = Process.Start(psi)!;
p.WaitForExit();
return p.ExitCode;
}
}