LD-KickWatch/src/KickStreamHub/Services/StartupService.cs

59 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}