Files
ThreadPilot/Services/UpdateInstallerService.cs
T
Prime Build 1540aeb7be Polish Windows 11 tweaks and add one-click updates (#40)
Verify persisted Windows tweak state on startup, modernize selection styling, and make in-app updates check every launch, install automatically after consent, preserve settings, and relaunch ThreadPilot.
2026-08-01 13:50:11 +02:00

75 lines
2.7 KiB
C#

/*
* ThreadPilot - elevated update installer launch.
*/
namespace ThreadPilot.Services
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
public sealed class UpdateInstallerService : IUpdateInstallerService
{
private static readonly string[] InstallerArguments =
{
"/VERYSILENT",
"/SUPPRESSMSGBOXES",
"/NORESTART",
"/CLOSEAPPLICATIONS",
"/RESTARTAPPLICATIONS",
"/THREADPILOTUPDATE=1",
};
private readonly IUpdateTempDirectoryProvider tempDirectoryProvider;
private readonly IUpdateProcessLauncher processLauncher;
public UpdateInstallerService(
IUpdateTempDirectoryProvider tempDirectoryProvider,
IUpdateProcessLauncher processLauncher)
{
this.tempDirectoryProvider = tempDirectoryProvider ?? throw new ArgumentNullException(nameof(tempDirectoryProvider));
this.processLauncher = processLauncher ?? throw new ArgumentNullException(nameof(processLauncher));
}
public Task LaunchInstallerElevatedAsync(string installerPath, CancellationToken cancellationToken = default)
{
if (!File.Exists(installerPath))
{
throw new FileNotFoundException("Update installer was not found.", installerPath);
}
if (!string.Equals(Path.GetExtension(installerPath), ".exe", StringComparison.OrdinalIgnoreCase) ||
!this.tempDirectoryProvider.IsSafeUpdateTempPath(installerPath))
{
throw new InvalidOperationException("Update installer path is not trusted.");
}
return this.processLauncher.LaunchElevatedAsync(installerPath, InstallerArguments, cancellationToken);
}
}
public sealed class ShellUpdateProcessLauncher : IUpdateProcessLauncher
{
public Task LaunchElevatedAsync(string fileName, IReadOnlyList<string> arguments, CancellationToken cancellationToken = default)
{
var startInfo = new ProcessStartInfo
{
FileName = fileName,
UseShellExecute = true,
Verb = "runas",
WorkingDirectory = Path.GetDirectoryName(fileName) ?? Environment.CurrentDirectory,
};
foreach (var argument in arguments)
{
startInfo.ArgumentList.Add(argument);
}
Process.Start(startInfo);
return Task.CompletedTask;
}
}
}