31 lines
1 KiB
C#
31 lines
1 KiB
C#
using Microsoft.Win32;
|
||
|
||
namespace KronaStaffTerminal.Services;
|
||
|
||
public static class StartupService
|
||
{
|
||
private const string RunKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
|
||
private const string ValueName = "KronaStaffTerminal";
|
||
|
||
public static void SetEnabled(bool enabled)
|
||
{
|
||
using var key = Registry.CurrentUser.OpenSubKey(RunKey, writable: true)
|
||
?? Registry.CurrentUser.CreateSubKey(RunKey, writable: true);
|
||
if (enabled)
|
||
{
|
||
var executablePath = Environment.ProcessPath
|
||
?? throw new InvalidOperationException("Не удалось определить путь программы.");
|
||
key.SetValue(ValueName, $"\"{executablePath}\" --autostart", RegistryValueKind.String);
|
||
}
|
||
else
|
||
{
|
||
key.DeleteValue(ValueName, throwOnMissingValue: false);
|
||
}
|
||
}
|
||
|
||
public static bool IsEnabled()
|
||
{
|
||
using var key = Registry.CurrentUser.OpenSubKey(RunKey);
|
||
return key?.GetValue(ValueName) is string;
|
||
}
|
||
}
|