Initial commit: ReminderOrganizer WPF app
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
55
Services/AutostartService.cs
Normal file
55
Services/AutostartService.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
#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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user