1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/GUI/StartupManager.cs Fri Mar 26 20:58:10 2010 +0000
1.3 @@ -0,0 +1,160 @@
1.4 +/*
1.5 +
1.6 + Version: MPL 1.1/GPL 2.0/LGPL 2.1
1.7 +
1.8 + The contents of this file are subject to the Mozilla Public License Version
1.9 + 1.1 (the "License"); you may not use this file except in compliance with
1.10 + the License. You may obtain a copy of the License at
1.11 +
1.12 + http://www.mozilla.org/MPL/
1.13 +
1.14 + Software distributed under the License is distributed on an "AS IS" basis,
1.15 + WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1.16 + for the specific language governing rights and limitations under the License.
1.17 +
1.18 + The Original Code is the Open Hardware Monitor code.
1.19 +
1.20 + The Initial Developer of the Original Code is
1.21 + Michael Möller <m.moeller@gmx.ch>.
1.22 + Portions created by the Initial Developer are Copyright (C) 2009-2010
1.23 + the Initial Developer. All Rights Reserved.
1.24 +
1.25 + Contributor(s):
1.26 +
1.27 + Alternatively, the contents of this file may be used under the terms of
1.28 + either the GNU General Public License Version 2 or later (the "GPL"), or
1.29 + the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
1.30 + in which case the provisions of the GPL or the LGPL are applicable instead
1.31 + of those above. If you wish to allow use of your version of this file only
1.32 + under the terms of either the GPL or the LGPL, and not to allow others to
1.33 + use your version of this file under the terms of the MPL, indicate your
1.34 + decision by deleting the provisions above and replace them with the notice
1.35 + and other provisions required by the GPL or the LGPL. If you do not delete
1.36 + the provisions above, a recipient may use your version of this file under
1.37 + the terms of any one of the MPL, the GPL or the LGPL.
1.38 +
1.39 +*/
1.40 +
1.41 +using System;
1.42 +using System.Collections.Generic;
1.43 +using System.IO;
1.44 +using System.Windows.Forms;
1.45 +using Microsoft.Win32;
1.46 +using OpenHardwareMonitor.TaskScheduler;
1.47 +
1.48 +namespace OpenHardwareMonitor.GUI {
1.49 + public class StartupManager {
1.50 +
1.51 + private TaskSchedulerClass scheduler;
1.52 + private bool startup;
1.53 +
1.54 + private const string REGISTRY_RUN =
1.55 + @"Software\Microsoft\Windows\CurrentVersion\Run";
1.56 +
1.57 + public StartupManager() {
1.58 + try {
1.59 + scheduler = new TaskSchedulerClass();
1.60 + scheduler.Connect(null, null, null, null);
1.61 + } catch (Exception) {
1.62 + scheduler = null;
1.63 + }
1.64 +
1.65 + if (scheduler != null) {
1.66 + try {
1.67 + ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
1.68 + IRegisteredTask task = folder.GetTask("Startup");
1.69 + startup = task != null;
1.70 + } catch (FileNotFoundException) {
1.71 + startup = false;
1.72 + }
1.73 + } else {
1.74 + RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN);
1.75 + startup = false;
1.76 + if (key != null) {
1.77 + string value = (string)key.GetValue("OpenHardwareMonitor");
1.78 + if (value != null)
1.79 + startup = value == Application.ExecutablePath;
1.80 + }
1.81 + }
1.82 + }
1.83 +
1.84 + private void CreateSchedulerTask() {
1.85 + ITaskDefinition definition = scheduler.NewTask(0);
1.86 + definition.RegistrationInfo.Description =
1.87 + "This task starts the Open Hardware Monitor on Windows startup.";
1.88 + definition.Principal.RunLevel =
1.89 + TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
1.90 + definition.Settings.DisallowStartIfOnBatteries = false;
1.91 + definition.Settings.StopIfGoingOnBatteries = false;
1.92 + definition.Settings.ExecutionTimeLimit = "PT0S";
1.93 +
1.94 + ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
1.95 + TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
1.96 +
1.97 + IExecAction action = (IExecAction)definition.Actions.Create(
1.98 + TASK_ACTION_TYPE.TASK_ACTION_EXEC);
1.99 + action.Path = Application.ExecutablePath;
1.100 + action.WorkingDirectory =
1.101 + Path.GetDirectoryName(Application.ExecutablePath);
1.102 +
1.103 + ITaskFolder root = scheduler.GetFolder("\\");
1.104 + ITaskFolder folder;
1.105 + try {
1.106 + folder = root.GetFolder("Open Hardware Monitor");
1.107 + } catch (FileNotFoundException) {
1.108 + folder = root.CreateFolder("Open Hardware Monitor", "");
1.109 + }
1.110 + folder.RegisterTaskDefinition("Startup", definition,
1.111 + (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
1.112 + TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
1.113 + }
1.114 +
1.115 + private void DeleteSchedulerTask() {
1.116 + ITaskFolder root = scheduler.GetFolder("\\");
1.117 + try {
1.118 + ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
1.119 + folder.DeleteTask("Startup", 0);
1.120 + } catch (FileNotFoundException) { }
1.121 + try {
1.122 + root.DeleteFolder("Open Hardware Monitor", 0);
1.123 + } catch (FileNotFoundException) { }
1.124 + }
1.125 +
1.126 + private void CreateRegistryRun() {
1.127 + RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
1.128 + key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
1.129 + }
1.130 +
1.131 + private void DeleteRegistryRun() {
1.132 + RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
1.133 + key.DeleteValue("OpenHardwareMonitor");
1.134 + }
1.135 +
1.136 + public bool Startup {
1.137 + get {
1.138 + return startup;
1.139 + }
1.140 + set {
1.141 + if (startup != value) {
1.142 + startup = value;
1.143 + if (scheduler != null) {
1.144 + if (startup)
1.145 + CreateSchedulerTask();
1.146 + else
1.147 + DeleteSchedulerTask();
1.148 + } else {
1.149 + if (startup)
1.150 + CreateRegistryRun();
1.151 + else
1.152 + DeleteRegistryRun();
1.153 + }
1.154 + }
1.155 + }
1.156 + }
1.157 + }
1.158 +
1.159 +
1.160 +
1.161 +
1.162 +
1.163 +}