Skip to content

Commit 35f4fd5

Browse files
committed
Fix restart as administrator issue
1 parent 52c36ff commit 35f4fd5

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.IO;
45
using System.Linq;
6+
using System.Reflection;
7+
using System.Threading;
58
using System.Threading.Tasks;
69
using System.Windows;
710
using System.Windows.Forms;
@@ -168,19 +171,43 @@ private async Task CheckAdminChangeAndAskForRestartAsync()
168171
await ImageLoader.WaitSaveAsync();
169172

170173
// Restart the app as administrator
171-
var startInfo = new ProcessStartInfo
172-
{
173-
UseShellExecute = true,
174-
WorkingDirectory = Environment.CurrentDirectory,
175-
FileName = Constant.ExecutablePath,
176-
Verb = "runas"
177-
};
178-
Process.Start(startInfo);
179-
Environment.Exit(0);
174+
RestartAppAsAdministrator(Constant.ExecutablePath);
180175
}
181176
}
182177
}
183178

179+
private static void RestartAppAsAdministrator(string exeToStart)
180+
{
181+
var startInfo = new ProcessStartInfo
182+
{
183+
FileName = getUpdateExe(),
184+
Arguments = $"--processStartAndWait {exeToStart}",
185+
UseShellExecute = true,
186+
Verb = "runas",
187+
};
188+
Process.Start(startInfo);
189+
Thread.Sleep(500);
190+
Environment.Exit(0);
191+
}
192+
193+
private static string getUpdateExe()
194+
{
195+
Assembly entryAssembly = Assembly.GetEntryAssembly();
196+
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)
197+
{
198+
return Path.GetFullPath(entryAssembly.Location);
199+
}
200+
201+
entryAssembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
202+
FileInfo fileInfo = new FileInfo(Path.Combine(Path.GetDirectoryName(entryAssembly.Location), "..\\Update.exe"));
203+
if (!fileInfo.Exists)
204+
{
205+
throw new Exception("Update.exe not found, not a Squirrel-installed app?");
206+
}
207+
208+
return fileInfo.FullName;
209+
}
210+
184211
public List<SearchWindowScreenData> SearchWindowScreens { get; } =
185212
DropdownDataGeneric<SearchWindowScreens>.GetValues<SearchWindowScreenData>("SearchWindowScreen");
186213

0 commit comments

Comments
 (0)