|
1 | 1 | using GHelper.Helpers;
|
2 | 2 | using Microsoft.Win32.TaskScheduler;
|
| 3 | +using System.Diagnostics; |
| 4 | +using System.Reflection; |
3 | 5 | using System.Security.Principal;
|
4 | 6 |
|
5 | 7 | public class Startup
|
@@ -42,9 +44,42 @@ public static void StartupCheck()
|
42 | 44 | try
|
43 | 45 | {
|
44 | 46 | string action = task.Definition.Actions.FirstOrDefault()!.ToString().Trim();
|
45 |
| - if (!strExeFilePath.Equals(action, StringComparison.OrdinalIgnoreCase) && !File.Exists(action)) |
| 47 | + bool needsReschedule = false; |
| 48 | + |
| 49 | + if (!strExeFilePath.Equals(action, StringComparison.OrdinalIgnoreCase)) |
| 50 | + { |
| 51 | + if (!File.Exists(action)) |
| 52 | + { |
| 53 | + Logger.WriteLine("Startup file doesn't exist: " + action); |
| 54 | + needsReschedule = true; |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + try |
| 59 | + { |
| 60 | + var currentVer = Assembly.GetEntryAssembly().GetName().Version; |
| 61 | + var fv = FileVersionInfo.GetVersionInfo(action).FileVersion.Split('.'); |
| 62 | + var scheduledVer = new Version( |
| 63 | + int.Parse(fv[0]), |
| 64 | + fv.Length > 1 ? int.Parse(fv[1]) : 0, |
| 65 | + fv.Length > 2 ? int.Parse(fv[2]) : 0, |
| 66 | + fv.Length > 3 ? int.Parse(fv[3]) : 0 |
| 67 | + ); |
| 68 | + if (currentVer > scheduledVer) |
| 69 | + { |
| 70 | + Logger.WriteLine($"Startup file is older {scheduledVer}, current is {currentVer}"); |
| 71 | + needsReschedule = true; |
| 72 | + } |
| 73 | + } |
| 74 | + catch (Exception ex) |
| 75 | + { |
| 76 | + Logger.WriteLine("Can't compare assembly versions: " + ex.Message); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (needsReschedule) |
46 | 82 | {
|
47 |
| - Logger.WriteLine("Startup file doesn't exist: " + action); |
48 | 83 | if (task.Definition.Principal.RunLevel == TaskRunLevel.Highest && !ProcessHelper.IsUserAdministrator())
|
49 | 84 | {
|
50 | 85 | ProcessHelper.RunAsAdmin();
|
|
0 commit comments