From 35f4fd5b187f7c7e7a2b289d674f51d103071ffd Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Fri, 23 May 2025 12:36:03 +0800 Subject: [PATCH] Fix restart as administrator issue --- .../SettingsPaneGeneralViewModel.cs | 45 +++++++++++++++---- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs index 8a3f1661f..6fab0cb2e 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs @@ -1,7 +1,10 @@ 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; @@ -168,19 +171,43 @@ public partial class SettingsPaneGeneralViewModel : BaseModel await ImageLoader.WaitSaveAsync(); // Restart the app as administrator - var startInfo = new ProcessStartInfo - { - UseShellExecute = true, - WorkingDirectory = Environment.CurrentDirectory, - FileName = Constant.ExecutablePath, - Verb = "runas" - }; - Process.Start(startInfo); - Environment.Exit(0); + RestartAppAsAdministrator(Constant.ExecutablePath); } } } + 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); + } + + private 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");