-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoStartService.cs
More file actions
47 lines (39 loc) · 1.29 KB
/
AutoStartService.cs
File metadata and controls
47 lines (39 loc) · 1.29 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
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;
namespace ESOServerStatusDaemon
{
public interface IAutoStartService
{
bool IsEnabled();
void Enable();
void Disable();
}
public sealed class AutoStartService : IAutoStartService
{
private const string RunKeyPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
private readonly string _appName = "ESOServerStatusDaemon";
private readonly string _exePath;
public AutoStartService()
{
_exePath = Process.GetCurrentProcess().MainModule?.FileName ?? System.Reflection.Assembly.GetEntryAssembly()!.Location;
}
public bool IsEnabled()
{
using var key = Registry.CurrentUser.OpenSubKey(RunKeyPath, false);
var value = key?.GetValue(_appName) as string;
return !string.IsNullOrEmpty(value);
}
public void Enable()
{
using var key = Registry.CurrentUser.CreateSubKey(RunKeyPath);
key.SetValue(_appName, '"' + _exePath + '"');
}
public void Disable()
{
using var key = Registry.CurrentUser.OpenSubKey(RunKeyPath, writable: true);
key?.DeleteValue(_appName, false);
}
}
}