Fix restart as administrator issue

This commit is contained in:
Jack251970 2025-05-23 12:36:03 +08:00
parent 52c36ff8d4
commit 35f4fd5b18

View file

@ -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<SearchWindowScreenData> SearchWindowScreens { get; } =
DropdownDataGeneric<SearchWindowScreens>.GetValues<SearchWindowScreenData>("SearchWindowScreen");