59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
using System.Diagnostics;
|
||
using System.IO;
|
||
using Microsoft.Win32;
|
||
|
||
namespace KickStreamHub.Services;
|
||
|
||
public static class StartupService
|
||
{
|
||
private const string AppName = "LDKickWatch";
|
||
private const string LegacyAppName = "KickStreamHub";
|
||
private const string RunRegistryKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
|
||
|
||
public static void SetRunOnStartup(bool enable)
|
||
{
|
||
try
|
||
{
|
||
using var key = Registry.CurrentUser.OpenSubKey(RunRegistryKey, writable: true);
|
||
if (key == null) return;
|
||
|
||
if (key.GetValue(LegacyAppName) != null)
|
||
{
|
||
key.DeleteValue(LegacyAppName, throwOnMissingValue: false);
|
||
}
|
||
|
||
if (enable)
|
||
{
|
||
var exePath = Environment.ProcessPath ?? Process.GetCurrentProcess().MainModule?.FileName;
|
||
if (!string.IsNullOrEmpty(exePath) && File.Exists(exePath))
|
||
{
|
||
key.SetValue(AppName, $"\"{exePath}\"");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (key.GetValue(AppName) != null)
|
||
{
|
||
key.DeleteValue(AppName, throwOnMissingValue: false);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LoggingService.Instance.Warning($"Başlangıçta çalıştırma ayarı güncellenemedi: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
public static bool IsRunOnStartup()
|
||
{
|
||
try
|
||
{
|
||
using var key = Registry.CurrentUser.OpenSubKey(RunRegistryKey, writable: false);
|
||
return key?.GetValue(AppName) != null;
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|