Added an option to automatically startup after Windows logon using the Task Scheduler 2.0 for Windows Vista/7 or the Registry for Windows XP.
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.Windows.Forms;
42 using Microsoft.Win32;
43 using OpenHardwareMonitor.TaskScheduler;
45 namespace OpenHardwareMonitor.GUI {
46 public class StartupManager {
48 private TaskSchedulerClass scheduler;
51 private const string REGISTRY_RUN =
52 @"Software\Microsoft\Windows\CurrentVersion\Run";
54 public StartupManager() {
56 scheduler = new TaskSchedulerClass();
57 scheduler.Connect(null, null, null, null);
62 if (scheduler != null) {
64 ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
65 IRegisteredTask task = folder.GetTask("Startup");
66 startup = task != null;
67 } catch (FileNotFoundException) {
71 RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN);
74 string value = (string)key.GetValue("OpenHardwareMonitor");
76 startup = value == Application.ExecutablePath;
81 private void CreateSchedulerTask() {
82 ITaskDefinition definition = scheduler.NewTask(0);
83 definition.RegistrationInfo.Description =
84 "This task starts the Open Hardware Monitor on Windows startup.";
85 definition.Principal.RunLevel =
86 TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
87 definition.Settings.DisallowStartIfOnBatteries = false;
88 definition.Settings.StopIfGoingOnBatteries = false;
89 definition.Settings.ExecutionTimeLimit = "PT0S";
91 ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
92 TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
94 IExecAction action = (IExecAction)definition.Actions.Create(
95 TASK_ACTION_TYPE.TASK_ACTION_EXEC);
96 action.Path = Application.ExecutablePath;
97 action.WorkingDirectory =
98 Path.GetDirectoryName(Application.ExecutablePath);
100 ITaskFolder root = scheduler.GetFolder("\\");
103 folder = root.GetFolder("Open Hardware Monitor");
104 } catch (FileNotFoundException) {
105 folder = root.CreateFolder("Open Hardware Monitor", "");
107 folder.RegisterTaskDefinition("Startup", definition,
108 (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
109 TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
112 private void DeleteSchedulerTask() {
113 ITaskFolder root = scheduler.GetFolder("\\");
115 ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
116 folder.DeleteTask("Startup", 0);
117 } catch (FileNotFoundException) { }
119 root.DeleteFolder("Open Hardware Monitor", 0);
120 } catch (FileNotFoundException) { }
123 private void CreateRegistryRun() {
124 RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
125 key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
128 private void DeleteRegistryRun() {
129 RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
130 key.DeleteValue("OpenHardwareMonitor");
133 public bool Startup {
138 if (startup != value) {
140 if (scheduler != null) {
142 CreateSchedulerTask();
144 DeleteSchedulerTask();