|
1 | 1 | using System; |
| 2 | +using System.IO; |
| 3 | +using System.Runtime.InteropServices; |
2 | 4 | using System.Security; |
| 5 | +using System.Security.Principal; |
3 | 6 | using System.Windows.Forms; |
4 | 7 | using Microsoft.Win32; |
| 8 | +using OpenHardwareMonitor.TaskScheduler; |
5 | 9 |
|
6 | 10 | namespace OpenHardwareMonitor.GUI { |
7 | | - |
8 | 11 | public class StartupManager { |
9 | 12 |
|
| 13 | + private TaskSchedulerClass scheduler; |
10 | 14 | private bool startup; |
| 15 | + private bool isAvailable; |
11 | 16 |
|
12 | | - private const string REGISTRY_RUN = @"Software\Microsoft\Windows\CurrentVersion\Run"; |
| 17 | + private const string REGISTRY_RUN = |
| 18 | + @"Software\Microsoft\Windows\CurrentVersion\Run"; |
13 | 19 |
|
14 | | - // private bool IsAdministrator() { |
15 | | - // try { |
16 | | - // WindowsIdentity identity = WindowsIdentity.GetCurrent(); |
17 | | - // WindowsPrincipal principal = new WindowsPrincipal(identity); |
18 | | - // return principal.IsInRole(WindowsBuiltInRole.Administrator); |
19 | | - // } catch { |
20 | | - // return false; |
21 | | - // } |
22 | | - // } |
| 20 | + private bool IsAdministrator() { |
| 21 | + try { |
| 22 | + WindowsIdentity identity = WindowsIdentity.GetCurrent(); |
| 23 | + WindowsPrincipal principal = new WindowsPrincipal(identity); |
| 24 | + return principal.IsInRole(WindowsBuiltInRole.Administrator); |
| 25 | + } catch { |
| 26 | + return false; |
| 27 | + } |
| 28 | + } |
23 | 29 |
|
24 | 30 | public StartupManager() { |
25 | 31 | if (Hardware.OperatingSystem.IsUnix) { |
26 | | - IsAvailable = false; |
| 32 | + scheduler = null; |
| 33 | + isAvailable = false; |
27 | 34 | return; |
28 | 35 | } |
29 | 36 |
|
30 | | - try { |
31 | | - using (var key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN)) { |
32 | | - startup = false; |
33 | | - var value = (string) key?.GetValue("OpenHardwareMonitor"); |
34 | | - if (value != null) |
35 | | - startup = value == Application.ExecutablePath; |
| 37 | + if (IsAdministrator()) { |
| 38 | + try { |
| 39 | + scheduler = new TaskSchedulerClass(); |
| 40 | + scheduler.Connect(null, null, null, null); |
| 41 | + } catch { |
| 42 | + scheduler = null; |
36 | 43 | } |
37 | | - IsAvailable = true; |
38 | | - } catch (SecurityException) { |
39 | | - IsAvailable = false; |
| 44 | + |
| 45 | + if (scheduler != null) { |
| 46 | + try { |
| 47 | + try { |
| 48 | + // check if the taskscheduler is running |
| 49 | + IRunningTaskCollection collection = scheduler.GetRunningTasks(0); |
| 50 | + } catch (ArgumentException) { } |
| 51 | + |
| 52 | + ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor"); |
| 53 | + IRegisteredTask task = folder.GetTask("Startup"); |
| 54 | + startup = (task != null) && |
| 55 | + (task.Definition.Triggers.Count > 0) && |
| 56 | + (task.Definition.Triggers[1].Type == |
| 57 | + TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON) && |
| 58 | + (task.Definition.Actions.Count > 0) && |
| 59 | + (task.Definition.Actions[1].Type == |
| 60 | + TASK_ACTION_TYPE.TASK_ACTION_EXEC) && |
| 61 | + (task.Definition.Actions[1] as IExecAction != null) && |
| 62 | + ((task.Definition.Actions[1] as IExecAction).Path == |
| 63 | + Application.ExecutablePath); |
| 64 | + |
| 65 | + } catch (IOException) { |
| 66 | + startup = false; |
| 67 | + } catch (UnauthorizedAccessException) { |
| 68 | + scheduler = null; |
| 69 | + } catch (COMException) { |
| 70 | + scheduler = null; |
| 71 | + } catch (NotImplementedException) { |
| 72 | + scheduler = null; |
| 73 | + } |
| 74 | + } |
| 75 | + } else { |
| 76 | + scheduler = null; |
| 77 | + } |
| 78 | + |
| 79 | + if (scheduler == null) { |
| 80 | + try { |
| 81 | + using (RegistryKey key = |
| 82 | + Registry.CurrentUser.OpenSubKey(REGISTRY_RUN)) { |
| 83 | + startup = false; |
| 84 | + if (key != null) { |
| 85 | + string value = (string)key.GetValue("OpenHardwareMonitor"); |
| 86 | + if (value != null) |
| 87 | + startup = value == Application.ExecutablePath; |
| 88 | + } |
| 89 | + } |
| 90 | + isAvailable = true; |
| 91 | + } catch (SecurityException) { |
| 92 | + isAvailable = false; |
| 93 | + } |
| 94 | + } else { |
| 95 | + isAvailable = true; |
40 | 96 | } |
41 | 97 | } |
42 | 98 |
|
43 | | - private static void CreateRegistryRun() { |
44 | | - var key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN); |
45 | | - key?.SetValue("OpenHardwareMonitor", Application.ExecutablePath); |
| 99 | + private void CreateSchedulerTask() { |
| 100 | + ITaskDefinition definition = scheduler.NewTask(0); |
| 101 | + definition.RegistrationInfo.Description = |
| 102 | + "This task starts the Open Hardware Monitor on Windows startup."; |
| 103 | + definition.Principal.RunLevel = |
| 104 | + TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST; |
| 105 | + definition.Settings.DisallowStartIfOnBatteries = false; |
| 106 | + definition.Settings.StopIfGoingOnBatteries = false; |
| 107 | + definition.Settings.ExecutionTimeLimit = "PT0S"; |
| 108 | + |
| 109 | + ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create( |
| 110 | + TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON); |
| 111 | + |
| 112 | + IExecAction action = (IExecAction)definition.Actions.Create( |
| 113 | + TASK_ACTION_TYPE.TASK_ACTION_EXEC); |
| 114 | + action.Path = Application.ExecutablePath; |
| 115 | + action.WorkingDirectory = |
| 116 | + Path.GetDirectoryName(Application.ExecutablePath); |
| 117 | + |
| 118 | + ITaskFolder root = scheduler.GetFolder("\\"); |
| 119 | + ITaskFolder folder; |
| 120 | + try { |
| 121 | + folder = root.GetFolder("Open Hardware Monitor"); |
| 122 | + } catch (IOException) { |
| 123 | + folder = root.CreateFolder("Open Hardware Monitor", ""); |
| 124 | + } |
| 125 | + folder.RegisterTaskDefinition("Startup", definition, |
| 126 | + (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, |
| 127 | + TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, ""); |
| 128 | + } |
| 129 | + |
| 130 | + private void DeleteSchedulerTask() { |
| 131 | + ITaskFolder root = scheduler.GetFolder("\\"); |
| 132 | + try { |
| 133 | + ITaskFolder folder = root.GetFolder("Open Hardware Monitor"); |
| 134 | + folder.DeleteTask("Startup", 0); |
| 135 | + } catch (IOException) { } |
| 136 | + try { |
| 137 | + root.DeleteFolder("Open Hardware Monitor", 0); |
| 138 | + } catch (IOException) { } |
46 | 139 | } |
47 | 140 |
|
48 | | - private static void DeleteRegistryRun() { |
49 | | - var key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN); |
50 | | - key?.DeleteValue("OpenHardwareMonitor"); |
| 141 | + private void CreateRegistryRun() { |
| 142 | + RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN); |
| 143 | + key.SetValue("OpenHardwareMonitor", Application.ExecutablePath); |
51 | 144 | } |
52 | 145 |
|
53 | | - public bool IsAvailable { get; } |
| 146 | + private void DeleteRegistryRun() { |
| 147 | + RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN); |
| 148 | + key.DeleteValue("OpenHardwareMonitor"); |
| 149 | + } |
| 150 | + |
| 151 | + public bool IsAvailable { |
| 152 | + get { return isAvailable; } |
| 153 | + } |
54 | 154 |
|
55 | 155 | public bool Startup { |
56 | | - get => startup; |
| 156 | + get { |
| 157 | + return startup; |
| 158 | + } |
57 | 159 | set { |
58 | | - if (startup == value) return; |
59 | | - if (!IsAvailable) |
60 | | - throw new InvalidOperationException(); |
61 | | - |
62 | | - try { |
63 | | - if (value) |
64 | | - CreateRegistryRun(); |
65 | | - else |
66 | | - DeleteRegistryRun(); |
67 | | - startup = value; |
68 | | - } |
69 | | - catch (UnauthorizedAccessException) { |
70 | | - throw new InvalidOperationException(); |
| 160 | + if (startup != value) { |
| 161 | + if (isAvailable) { |
| 162 | + if (scheduler != null) { |
| 163 | + if (value) |
| 164 | + CreateSchedulerTask(); |
| 165 | + else |
| 166 | + DeleteSchedulerTask(); |
| 167 | + startup = value; |
| 168 | + } else { |
| 169 | + try { |
| 170 | + if (value) |
| 171 | + CreateRegistryRun(); |
| 172 | + else |
| 173 | + DeleteRegistryRun(); |
| 174 | + startup = value; |
| 175 | + } catch (UnauthorizedAccessException) { |
| 176 | + throw new InvalidOperationException(); |
| 177 | + } |
| 178 | + } |
| 179 | + } else { |
| 180 | + throw new InvalidOperationException(); |
| 181 | + } |
71 | 182 | } |
72 | 183 | } |
73 | 184 | } |
|
0 commit comments