From 479572e39d19a1e9aeb775b401dce70838e24b19 Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Sun, 7 May 2023 00:24:47 +0200 Subject: [PATCH] Startup admin restarter --- app/Program.cs | 2 +- app/Startup.cs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/Program.cs b/app/Program.cs index 3a71b39a..e65a9637 100644 --- a/app/Program.cs +++ b/app/Program.cs @@ -345,7 +345,7 @@ namespace GHelper } } - static bool IsUserAdministrator() + public static bool IsUserAdministrator() { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); diff --git a/app/Startup.cs b/app/Startup.cs index c78fb549..139b4073 100644 --- a/app/Startup.cs +++ b/app/Startup.cs @@ -1,4 +1,5 @@ -using Microsoft.Win32.TaskScheduler; +using GHelper; +using Microsoft.Win32.TaskScheduler; using System.Diagnostics; using System.Security.Principal; @@ -28,6 +29,9 @@ public class Startup td.Triggers.Add(new LogonTrigger { UserId = userId }); td.Actions.Add(strExeFilePath); + if (Program.IsUserAdministrator()) + td.Principal.RunLevel = TaskRunLevel.Highest; + td.Settings.StopIfGoingOnBatteries = false; td.Settings.DisallowStartIfOnBatteries = false; td.Settings.ExecutionTimeLimit = TimeSpan.Zero; @@ -41,7 +45,10 @@ public class Startup } catch (Exception e) { - MessageBox.Show("Can't create a start up task. Try running Task Scheduler by hand and manually deleting GHelper task if it exists there.", "Scheduler Error", MessageBoxButtons.OK); + if (Program.IsUserAdministrator()) + MessageBox.Show("Can't create a start up task. Try running Task Scheduler by hand and manually deleting GHelper task if it exists there.", "Scheduler Error", MessageBoxButtons.OK); + else + Program.RunAsAdmin(); } } @@ -57,7 +64,10 @@ public class Startup } catch (Exception e) { - MessageBox.Show("Can't remove task. Try running Task Scheduler by hand and manually deleting GHelper task if it exists there.", "Scheduler Error", MessageBoxButtons.OK); + if (Program.IsUserAdministrator()) + MessageBox.Show("Can't remove task. Try running Task Scheduler by hand and manually deleting GHelper task if it exists there.", "Scheduler Error", MessageBoxButtons.OK); + else + Program.RunAsAdmin(); } } }