|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Diagnostics; |
| 4 | +using System.IO; |
4 | 5 | using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | +using System.Threading; |
5 | 8 | using System.Threading.Tasks; |
6 | 9 | using System.Windows; |
7 | 10 | using System.Windows.Forms; |
@@ -168,19 +171,43 @@ private async Task CheckAdminChangeAndAskForRestartAsync() |
168 | 171 | await ImageLoader.WaitSaveAsync(); |
169 | 172 |
|
170 | 173 | // 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); |
180 | 175 | } |
181 | 176 | } |
182 | 177 | } |
183 | 178 |
|
| 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 | + |
184 | 211 | public List<SearchWindowScreenData> SearchWindowScreens { get; } = |
185 | 212 | DropdownDataGeneric<SearchWindowScreens>.GetValues<SearchWindowScreenData>("SearchWindowScreen"); |
186 | 213 |
|
|
0 commit comments