fix(startup): Start with Windows honours an entry Task Manager turned off

Task Manager's Startup tab keeps the Run value and writes a StartupApproved\Run value whose odd first byte means
off. The client and the Uploader now show such an entry as off; ticking the box removes the marker so the entry
runs again, unticking removes it with the Run value, and re-pointing the command after an update leaves it as it
is. Core/IO/StartupApproval holds the rule; the registry hive is injectable for tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 16:02:23 +00:00
1 parent b344d2a527
commit 8eaaef5085
5 files changed
+231 -47

No files matched your search

@@ -1,3 +1,4 @@
using System.IO;
using System.Security;
using Microsoft.Win32;
using Titano.Core.Diagnostics;
@@ -15,49 +16,65 @@ public interface IStartupRegistration
/// <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. Uninstalling removes the value when
/// it starts the Uploader the uninstaller removes (<see cref="StartupCommand"/> names it for both).
/// The registry stays the source of truth because Task Manager can turn it off: an entry it turned off
/// (<see cref="StartupApproval"/>) shows as off, and ticking the box turns it on again. Uninstalling removes the value
/// when it starts the Uploader the uninstaller removes (<see cref="StartupCommand"/> names it for both).
/// </summary>
public sealed class RunKeyStartupRegistration : IStartupRegistration
/// <param name="executable">The Uploader's program; the running one by default.</param>
/// <param name="root">The registry hive holding the entries; <c>HKEY_CURRENT_USER</c> by default (tests pass a scratch key).</param>
public sealed class RunKeyStartupRegistration(string? executable = null, RegistryKey? root = null) : IStartupRegistration
{
private const string RunKey = StartupCommand.RunKey;
private const string ValueName = StartupCommand.UploaderValue;
public bool IsEnabled
private readonly string? _executable = executable ?? Environment.ProcessPath;
private readonly RegistryKey _root = root ?? Registry.CurrentUser;
/// <summary>The entry exists and Task Manager has not turned it off.</summary>
public bool IsEnabled => HasEntry && !StartupApproval.IsTurnedOff(Read(StartupApproval.RunKey) as byte[]);
private bool HasEntry => Read(RunKey) is string { Length: > 0 };
private string Command => StartupCommand.Format(_executable ?? string.Empty, CommandLine.TrayOption);
public void SetEnabled(bool enabled) => Update(key =>
{
get
{
try
{
using var key = Registry.CurrentUser.OpenSubKey(RunKey);
return key?.GetValue(ValueName) is string { Length: > 0 };
}
catch (SecurityException)
{
return false;
}
}
if (enabled) key.SetValue(ValueName, Command, RegistryValueKind.String);
else key.DeleteValue(ValueName, throwOnMissingValue: false);
// Task Manager's "Disable" outlives a new command: ticking the box undoes it, and unticking leaves nothing behind.
using var approval = _root.OpenSubKey(StartupApproval.RunKey, writable: true);
approval?.DeleteValue(ValueName, throwOnMissingValue: false);
});
/// <summary>Points an existing entry at the current executable (after an update moved it); one Task Manager turned off stays off.</summary>
public void Refresh()
{
if (HasEntry) Update(key => key.SetValue(ValueName, Command, RegistryValueKind.String));
}
private static string Command => $"\"{Environment.ProcessPath}\" {CommandLine.TrayOption}";
public void SetEnabled(bool enabled)
private object? Read(string subKey)
{
try
{
using var key = Registry.CurrentUser.CreateSubKey(RunKey, writable: true);
if (enabled) key.SetValue(ValueName, Command, RegistryValueKind.String);
else key.DeleteValue(ValueName, throwOnMissingValue: false);
using var key = _root.OpenSubKey(subKey);
return key?.GetValue(ValueName);
}
catch (Exception ex) when (ex is SecurityException or UnauthorizedAccessException)
catch (Exception ex) when (ex is SecurityException or IOException)
{
return null;
}
}
private void Update(Action<RegistryKey> change)
{
try
{
using var key = _root.CreateSubKey(RunKey, writable: true);
change(key);
}
catch (Exception ex) when (ex is SecurityException or UnauthorizedAccessException or IOException)
{
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);
}
}