Skip to content

Commit d6462f4

Browse files
committed
Move restart function to app class
1 parent eacccf9 commit d6462f4

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

Flow.Launcher/App.xaml.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Diagnostics;
3+
using System.IO;
4+
using System.Reflection;
35
using System.Text;
46
using System.Threading;
57
using System.Threading.Tasks;
@@ -319,6 +321,45 @@ private static void RegisterTaskSchedulerUnhandledException()
319321

320322
#endregion
321323

324+
#region Restart
325+
326+
// Since Squirrel does not provide a way to restart the app as administrator,
327+
// we need to do it manually by starting the update.exe with the runas verb
328+
public static void RestartAppAsAdministrator()
329+
{
330+
var startInfo = new ProcessStartInfo
331+
{
332+
FileName = getUpdateExe(),
333+
Arguments = $"--processStartAndWait {Constant.ExecutablePath}",
334+
UseShellExecute = true,
335+
Verb = "runas",
336+
};
337+
Process.Start(startInfo);
338+
Thread.Sleep(500);
339+
Environment.Exit(0);
340+
341+
// Local function
342+
static string getUpdateExe()
343+
{
344+
Assembly entryAssembly = Assembly.GetEntryAssembly();
345+
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)
346+
{
347+
return Path.GetFullPath(entryAssembly.Location);
348+
}
349+
350+
entryAssembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
351+
FileInfo fileInfo = new FileInfo(Path.Combine(Path.GetDirectoryName(entryAssembly.Location), "..\\Update.exe"));
352+
if (!fileInfo.Exists)
353+
{
354+
throw new Exception("Update.exe not found, not a Squirrel-installed app?");
355+
}
356+
357+
return fileInfo.FullName;
358+
}
359+
}
360+
361+
#endregion
362+
322363
#region IDisposable
323364

324365
protected virtual void Dispose(bool disposing)

Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.IO;
53
using System.Linq;
6-
using System.Reflection;
7-
using System.Threading;
84
using System.Threading.Tasks;
95
using System.Windows;
106
using System.Windows.Forms;
@@ -171,46 +167,11 @@ private async Task CheckAdminChangeAndAskForRestartAsync()
171167
await ImageLoader.WaitSaveAsync();
172168

173169
// Restart the app as administrator
174-
RestartAppAsAdministrator(Constant.ExecutablePath);
170+
App.RestartAppAsAdministrator();
175171
}
176172
}
177173
}
178174

179-
// Since Squirrel does not provide a way to restart the app as administrator,
180-
// we need to do it manually by starting the update.exe with the runas verb
181-
private static void RestartAppAsAdministrator(string exeToStart)
182-
{
183-
var startInfo = new ProcessStartInfo
184-
{
185-
FileName = getUpdateExe(),
186-
Arguments = $"--processStartAndWait {exeToStart}",
187-
UseShellExecute = true,
188-
Verb = "runas",
189-
};
190-
Process.Start(startInfo);
191-
Thread.Sleep(500);
192-
Environment.Exit(0);
193-
194-
// Local function
195-
static string getUpdateExe()
196-
{
197-
Assembly entryAssembly = Assembly.GetEntryAssembly();
198-
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)
199-
{
200-
return Path.GetFullPath(entryAssembly.Location);
201-
}
202-
203-
entryAssembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
204-
FileInfo fileInfo = new FileInfo(Path.Combine(Path.GetDirectoryName(entryAssembly.Location), "..\\Update.exe"));
205-
if (!fileInfo.Exists)
206-
{
207-
throw new Exception("Update.exe not found, not a Squirrel-installed app?");
208-
}
209-
210-
return fileInfo.FullName;
211-
}
212-
}
213-
214175
public List<SearchWindowScreenData> SearchWindowScreens { get; } =
215176
DropdownDataGeneric<SearchWindowScreens>.GetValues<SearchWindowScreenData>("SearchWindowScreen");
216177

0 commit comments

Comments
 (0)