From d6462f4fdc2c28f8b14c8bbd879a3d8c15176dc4 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Fri, 23 May 2025 13:18:19 +0800 Subject: [PATCH] Move restart function to app class --- Flow.Launcher/App.xaml.cs | 41 +++++++++++++++++++ .../SettingsPaneGeneralViewModel.cs | 41 +------------------ 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 8c5915cd4..7a33c995f 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -1,5 +1,7 @@ using System; using System.Diagnostics; +using System.IO; +using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -319,6 +321,45 @@ namespace Flow.Launcher #endregion + #region Restart + + // 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() + { + var startInfo = new ProcessStartInfo + { + FileName = getUpdateExe(), + Arguments = $"--processStartAndWait {Constant.ExecutablePath}", + UseShellExecute = true, + Verb = "runas", + }; + Process.Start(startInfo); + Thread.Sleep(500); + Environment.Exit(0); + + // Local function + static string getUpdateExe() + { + Assembly entryAssembly = Assembly.GetEntryAssembly(); + if (entryAssembly != null && Path.GetFileName(entryAssembly.Location).Equals("update.exe", StringComparison.OrdinalIgnoreCase) && entryAssembly.Location.IndexOf("app-", StringComparison.OrdinalIgnoreCase) == -1 && entryAssembly.Location.IndexOf("SquirrelTemp", StringComparison.OrdinalIgnoreCase) == -1) + { + return Path.GetFullPath(entryAssembly.Location); + } + + entryAssembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly(); + FileInfo fileInfo = new FileInfo(Path.Combine(Path.GetDirectoryName(entryAssembly.Location), "..\\Update.exe")); + if (!fileInfo.Exists) + { + throw new Exception("Update.exe not found, not a Squirrel-installed app?"); + } + + return fileInfo.FullName; + } + } + + #endregion + #region IDisposable protected virtual void Dispose(bool disposing) diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs index 89a89b090..e7c7333c2 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs @@ -1,10 +1,6 @@ using System; using System.Collections.Generic; -using System.Diagnostics; -using System.IO; using System.Linq; -using System.Reflection; -using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; @@ -171,46 +167,11 @@ public partial class SettingsPaneGeneralViewModel : BaseModel await ImageLoader.WaitSaveAsync(); // Restart the app as administrator - RestartAppAsAdministrator(Constant.ExecutablePath); + App.RestartAppAsAdministrator(); } } } - // 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 - private static void RestartAppAsAdministrator(string exeToStart) - { - var startInfo = new ProcessStartInfo - { - FileName = getUpdateExe(), - Arguments = $"--processStartAndWait {exeToStart}", - UseShellExecute = true, - Verb = "runas", - }; - Process.Start(startInfo); - Thread.Sleep(500); - Environment.Exit(0); - - // Local function - static string getUpdateExe() - { - Assembly entryAssembly = Assembly.GetEntryAssembly(); - if (entryAssembly != null && Path.GetFileName(entryAssembly.Location).Equals("update.exe", StringComparison.OrdinalIgnoreCase) && entryAssembly.Location.IndexOf("app-", StringComparison.OrdinalIgnoreCase) == -1 && entryAssembly.Location.IndexOf("SquirrelTemp", StringComparison.OrdinalIgnoreCase) == -1) - { - return Path.GetFullPath(entryAssembly.Location); - } - - entryAssembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly(); - FileInfo fileInfo = new FileInfo(Path.Combine(Path.GetDirectoryName(entryAssembly.Location), "..\\Update.exe")); - if (!fileInfo.Exists) - { - throw new Exception("Update.exe not found, not a Squirrel-installed app?"); - } - - return fileInfo.FullName; - } - } - public List SearchWindowScreens { get; } = DropdownDataGeneric.GetValues("SearchWindowScreen");