Files
ReminderOrganizer/Services/AutostartService.cs

56 lines
1.5 KiB
C#
Raw Normal View History

#nullable enable
using System;
using Microsoft.Win32;
namespace ReminderOrganizer.Services
{
public class AutostartService : IAutostartService
{
private const string RegistryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
private const string AppName = "ReminderOrganizer";
public bool IsAutostartEnabled
{
get
{
try
{
using var key = Registry.CurrentUser.OpenSubKey(RegistryKey, false);
var path = key?.GetValue(AppName) as string;
return path == Environment.ProcessPath;
}
catch
{
return false;
}
}
}
public void ToggleAutostart()
{
try
{
using var key = Registry.CurrentUser.OpenSubKey(RegistryKey, true);
if (key == null) return;
if (IsAutostartEnabled)
{
key.DeleteValue(AppName, false);
}
else
{
var path = Environment.ProcessPath;
if (!string.IsNullOrEmpty(path))
{
key.SetValue(AppName, path);
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[AutostartService] Failed: {ex.Message}");
}
}
}
}