Fixed Issue 76.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
41 using System.Runtime.InteropServices;
42 using System.Security.Principal;
43 using System.Windows.Forms;
44 using Microsoft.Win32;
45 using OpenHardwareMonitor.TaskScheduler;
47 namespace OpenHardwareMonitor.GUI {
48 public class StartupManager {
50 private TaskSchedulerClass scheduler;
53 private const string REGISTRY_RUN =
54 @"Software\Microsoft\Windows\CurrentVersion\Run";
56 private bool IsAdministrator() {
58 WindowsIdentity identity = WindowsIdentity.GetCurrent();
59 WindowsPrincipal principal = new WindowsPrincipal(identity);
60 return principal.IsInRole(WindowsBuiltInRole.Administrator);
66 public StartupManager() {
67 if (IsAdministrator()) {
69 scheduler = new TaskSchedulerClass();
70 scheduler.Connect(null, null, null, null);
75 if (scheduler != null) {
77 // check if the taskscheduler is running
78 IRunningTaskCollection collection = scheduler.GetRunningTasks(0);
80 ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
81 IRegisteredTask task = folder.GetTask("Startup");
82 startup = (task != null) &&
83 (task.Definition.Triggers.Count > 0) &&
84 (task.Definition.Triggers[1].Type ==
85 TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON) &&
86 (task.Definition.Actions.Count > 0) &&
87 (task.Definition.Actions[1].Type ==
88 TASK_ACTION_TYPE.TASK_ACTION_EXEC) &&
89 (task.Definition.Actions[1] as IExecAction != null) &&
90 ((task.Definition.Actions[1] as IExecAction).Path ==
91 Application.ExecutablePath);
93 } catch (IOException) {
95 } catch (UnauthorizedAccessException) {
97 } catch (COMException) {
105 if (scheduler == null) {
106 RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN);
109 string value = (string)key.GetValue("OpenHardwareMonitor");
111 startup = value == Application.ExecutablePath;
116 private void CreateSchedulerTask() {
117 ITaskDefinition definition = scheduler.NewTask(0);
118 definition.RegistrationInfo.Description =
119 "This task starts the Open Hardware Monitor on Windows startup.";
120 definition.Principal.RunLevel =
121 TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
122 definition.Settings.DisallowStartIfOnBatteries = false;
123 definition.Settings.StopIfGoingOnBatteries = false;
124 definition.Settings.ExecutionTimeLimit = "PT0S";
126 ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
127 TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
129 IExecAction action = (IExecAction)definition.Actions.Create(
130 TASK_ACTION_TYPE.TASK_ACTION_EXEC);
131 action.Path = Application.ExecutablePath;
132 action.WorkingDirectory =
133 Path.GetDirectoryName(Application.ExecutablePath);
135 ITaskFolder root = scheduler.GetFolder("\\");
138 folder = root.GetFolder("Open Hardware Monitor");
139 } catch (IOException) {
140 folder = root.CreateFolder("Open Hardware Monitor", "");
142 folder.RegisterTaskDefinition("Startup", definition,
143 (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
144 TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
147 private void DeleteSchedulerTask() {
148 ITaskFolder root = scheduler.GetFolder("\\");
150 ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
151 folder.DeleteTask("Startup", 0);
152 } catch (IOException) { }
154 root.DeleteFolder("Open Hardware Monitor", 0);
155 } catch (IOException) { }
158 private void CreateRegistryRun() {
159 RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
160 key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
163 private void DeleteRegistryRun() {
164 RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
165 key.DeleteValue("OpenHardwareMonitor");
168 public bool Startup {
173 if (startup != value) {
175 if (scheduler != null) {
177 CreateSchedulerTask();
179 DeleteSchedulerTask();