uploader.json (server, user, DPAPI CurrentUser-protected token, trusted fingerprints, options, watch folders) written atomically; command line (paths, --tray, --capture); named mutex plus a current-user named pipe that hands a second launch's paths to the running instance; HKCU Run key for "Start with Windows"; coalescing background writer for state files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
62 lines
1.8 KiB
C#
62 lines
1.8 KiB
C#
using System.Security;
|
|
using Microsoft.Win32;
|
|
using Titano.Core.Diagnostics;
|
|
|
|
namespace Titano.Uploader.Infrastructure;
|
|
|
|
/// <summary>"Start with Windows".</summary>
|
|
public interface IStartupRegistration
|
|
{
|
|
bool IsEnabled { get; }
|
|
|
|
void SetEnabled(bool enabled);
|
|
}
|
|
|
|
/// <summary>
|
|
/// <c>HKCU\Software\Microsoft\Windows\CurrentVersion\Run</c> value that starts the uploader in the tray at sign-in.
|
|
/// The registry stays the source of truth because Task Manager can turn it off.
|
|
/// </summary>
|
|
public sealed class RunKeyStartupRegistration : IStartupRegistration
|
|
{
|
|
private const string RunKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
|
|
private const string ValueName = "Titano Uploader";
|
|
|
|
public bool IsEnabled
|
|
{
|
|
get
|
|
{
|
|
try
|
|
{
|
|
using var key = Registry.CurrentUser.OpenSubKey(RunKey);
|
|
return key?.GetValue(ValueName) is string { Length: > 0 };
|
|
}
|
|
catch (SecurityException)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
private static string Command => $"\"{Environment.ProcessPath}\" {CommandLine.TrayOption}";
|
|
|
|
public void SetEnabled(bool enabled)
|
|
{
|
|
try
|
|
{
|
|
using var key = Registry.CurrentUser.CreateSubKey(RunKey, writable: true);
|
|
if (enabled) key.SetValue(ValueName, Command, RegistryValueKind.String);
|
|
else key.DeleteValue(ValueName, throwOnMissingValue: false);
|
|
}
|
|
catch (Exception ex) when (ex is SecurityException or UnauthorizedAccessException)
|
|
{
|
|
Log.Warning("startup", "Run key not updated", ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>Points an existing entry at the current executable (after an update moved it).</summary>
|
|
public void Refresh()
|
|
{
|
|
if (IsEnabled) SetEnabled(true);
|
|
}
|
|
}
|