Files
ReminderOrganizer/Services/AutostartService.cs
Michi 1918712f7f Fix data loss risk, reminder bugs; theme, credits, tests
- Store tasks in %APPDATA% (single-file extract dir lost data on update),
  atomic serialized saves, corrupt-file backup, migration of old tasks.json
- Reminders without description never showed (empty balloon text threw)
- Catch up missed reminders on start; monthly recurrence no longer drifts
- Task dialog: date pick kept 00:00, cancel leaked recurrence changes
- CSV import understands quoted fields written by export
- Confirm before deleting; name folders on create; tasks sorted by date
- Single instance, autostart starts hidden in tray, tray icon disposed
- Theme helper for all dialogs, MDL2/Path icons instead of emojis,
  fixed folder selection/badge, localized counts and messages
- Credits (c) Michael Media + Discord link, exe Company/Copyright
- Locales embedded; xUnit tests (24); README, CLAUDE.md, sprite-generator

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 14:08:53 +01:00

75 lines
2.4 KiB
C#

#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";
/// <summary>Registry value: quoted exe path + --minimized (start in tray).</summary>
public static string BuildCommand(string exePath) => $"\"{exePath}\" --minimized";
/// <summary>True if the stored command points at this exe (old entries without quotes count too).</summary>
public static bool PointsTo(string? stored, string? exePath)
{
if (string.IsNullOrEmpty(stored) || string.IsNullOrEmpty(exePath)) return false;
var s = stored.Trim();
if (s.StartsWith("\""))
{
var end = s.IndexOf('"', 1);
s = end > 0 ? s.Substring(1, end - 1) : s.Trim('"');
}
else if (s.EndsWith(" --minimized"))
{
s = s[..^" --minimized".Length];
}
return string.Equals(s, exePath, StringComparison.OrdinalIgnoreCase);
}
public bool IsAutostartEnabled
{
get
{
try
{
using var key = Registry.CurrentUser.OpenSubKey(RegistryKey, false);
return PointsTo(key?.GetValue(AppName) as string, 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, BuildCommand(path));
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[AutostartService] Failed: {ex.Message}");
}
}
}
}