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(); } } }