Skip to content

Commit

Permalink
2024-08-25 - Reg key update
Browse files Browse the repository at this point in the history
If the program is updated via the updater, instead of the installer, the uninstaller and Windows should show the correct versions. #355
  • Loading branch information
TCNOco committed Aug 25, 2024
1 parent f3ed313 commit 9ab97d4
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions TcNo-Acc-Switcher-Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Win32;
using TcNo_Acc_Switcher_Globals;
using TcNo_Acc_Switcher_Server.Data;
using TcNo_Acc_Switcher_Server.Data.Settings;
Expand Down Expand Up @@ -112,6 +115,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

// Increment launch count. I don't know if this should be here, but it is.
AppStats.LaunchCount++;

// Update installed version number, if uninstaller preset.
if (OperatingSystem.IsWindows()) UpdateRegistryVersion(Globals.Version);
}

public static void CurrentDomain_OnProcessExit(object sender, EventArgs e)
Expand All @@ -131,5 +137,41 @@ private static void MoveIfFileExists(string f)
Globals.CopyFile(Path.Join(Globals.AppDataFolder, f), Path.Join(Globals.UserDataFolder, f));
Globals.DeleteFile(Path.Join(Globals.AppDataFolder, f));
}

private static readonly string[] RegistryKeys =
[
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\TcNo-Acc-Switcher",
@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\TcNo-Acc-Switcher"
];

[SupportedOSPlatform("windows")]
public static void UpdateRegistryVersion(string version)
{
version = version.Replace("-", ".").Replace("_", ".");

string exeDirectory = AppDomain.CurrentDomain.BaseDirectory;
string uninstallExePath = Path.Combine(exeDirectory, "Uninstall TcNo Account Switcher.exe");

// Check if uninstaller exists. If not, this copy isn't installed. Portable, or otherwise.
if (!File.Exists(uninstallExePath)) return;

foreach (var key in RegistryKeys)
{
try
{
using var registryKey = Registry.LocalMachine.OpenSubKey(key, writable: true);
if (registryKey == null) continue;

registryKey.SetValue("DisplayVersion", version, RegistryValueKind.String);
registryKey.SetValue("ProductVersion", version, RegistryValueKind.String);
registryKey.SetValue("FileVersion", version, RegistryValueKind.String);
Globals.WriteToLog($"Updated registry key: {key}");
}
catch (Exception ex)
{
Globals.WriteToLog($"Failed to update registry key {key}: {ex.Message}");
}
}
}
}
}

0 comments on commit 9ab97d4

Please sign in to comment.