From b2edea42019b50cb02f085b8bec4010e52cce605 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 3 Jun 2025 17:21:48 +0800 Subject: [PATCH] Improve code quality --- Flow.Launcher/App.xaml.cs | 21 ++++++++++++++++++++- Flow.Launcher/PublicAPIInstance.cs | 17 ++++------------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index c3dc03ed7..69e57f443 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -26,6 +26,7 @@ using Flow.Launcher.ViewModel; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.VisualStudio.Threading; +using Squirrel; namespace Flow.Launcher { @@ -334,9 +335,27 @@ namespace Flow.Launcher #region Restart + /// + /// Restart the application without changing the user privileges. + /// + public static void RestartApp(bool forceAdmin = false) + { + if (Win32Helper.IsAdministrator() || forceAdmin) + { + RestartAppAsAdministrator(); + } + else + { + // Restart requires Squirrel's Update.exe to be present in the parent folder, + // it is only published from the project's release pipeline. When debugging without it, + // the project may not restart or just terminates. This is expected. + UpdateManager.RestartApp(Constant.ApplicationFileName); + } + } + // Since Squirrel does not provide a way to restart the app as administrator, // we need to do it manually by starting the update.exe with the runas verb - public static void RestartAppAsAdministrator() + private static void RestartAppAsAdministrator() { var startInfo = new ProcessStartInfo { diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 5f427f902..a470b4739 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -31,7 +31,6 @@ using Flow.Launcher.Plugin.SharedModels; using Flow.Launcher.Plugin.SharedCommands; using Flow.Launcher.ViewModel; using JetBrains.Annotations; -using Squirrel; using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch; namespace Flow.Launcher @@ -85,18 +84,10 @@ namespace Flow.Launcher // Wait for all image caches to be saved before restarting await ImageLoader.WaitSaveAsync(); - // If app is run as administrator already, we continue to restart app as administrator - if (Win32Helper.IsAdministrator()) - { - App.RestartAppAsAdministrator(); - } - else - { - // Restart requires Squirrel's Update.exe to be present in the parent folder, - // it is only published from the project's release pipeline. When debugging without it, - // the project may not restart or just terminates. This is expected. - UpdateManager.RestartApp(Constant.ApplicationFileName); - } + // Restart requires Squirrel's Update.exe to be present in the parent folder, + // it is only published from the project's release pipeline. When debugging without it, + // the project may not restart or just terminates. This is expected. + App.RestartApp(); } public void ShowMainWindow() => _mainVM.Show();