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