GUI/StartupManager.cs
author moel.mich
Tue, 18 May 2010 19:12:13 +0000
changeset 120 41dc766e28b8
parent 104 4a9f8154d8f0
child 123 912a06e2bd53
permissions -rw-r--r--
Fixed Issue 60.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     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
     8  
     9   http://www.mozilla.org/MPL/
    10 
    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.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    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.
    21 
    22   Contributor(s):
    23 
    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.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.IO;
    41 using System.Windows.Forms;
    42 using Microsoft.Win32;
    43 using OpenHardwareMonitor.TaskScheduler;
    44 
    45 namespace OpenHardwareMonitor.GUI {
    46   public class StartupManager {
    47 
    48     private TaskSchedulerClass scheduler;
    49     private bool startup;
    50 
    51     private const string REGISTRY_RUN =
    52       @"Software\Microsoft\Windows\CurrentVersion\Run";
    53 
    54     public StartupManager() {
    55       try {
    56         scheduler = new TaskSchedulerClass();
    57         scheduler.Connect(null, null, null, null);
    58       } catch {
    59         scheduler = null;
    60       }
    61 
    62       if (scheduler != null) {
    63         try {
    64           ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
    65           IRegisteredTask task = folder.GetTask("Startup");
    66           startup = task != null;
    67         } catch (IOException) {
    68           startup = false;
    69         } catch (UnauthorizedAccessException) {
    70           scheduler = null;
    71         }
    72       } 
    73             
    74       if (scheduler == null) {
    75         RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN);
    76         startup = false;
    77         if (key != null) {
    78           string value = (string)key.GetValue("OpenHardwareMonitor");
    79           if (value != null)
    80             startup = value == Application.ExecutablePath;
    81         }
    82       }
    83     }
    84 
    85     private void CreateSchedulerTask() {
    86       ITaskDefinition definition = scheduler.NewTask(0);
    87       definition.RegistrationInfo.Description =
    88         "This task starts the Open Hardware Monitor on Windows startup.";
    89       definition.Principal.RunLevel =
    90         TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
    91       definition.Settings.DisallowStartIfOnBatteries = false;
    92       definition.Settings.StopIfGoingOnBatteries = false;
    93       definition.Settings.ExecutionTimeLimit = "PT0S";
    94 
    95       ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
    96         TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
    97 
    98       IExecAction action = (IExecAction)definition.Actions.Create(
    99         TASK_ACTION_TYPE.TASK_ACTION_EXEC);
   100       action.Path = Application.ExecutablePath;
   101       action.WorkingDirectory =
   102         Path.GetDirectoryName(Application.ExecutablePath);
   103 
   104       ITaskFolder root = scheduler.GetFolder("\\");
   105       ITaskFolder folder;
   106       try {
   107         folder = root.GetFolder("Open Hardware Monitor");
   108       } catch (IOException) {
   109         folder = root.CreateFolder("Open Hardware Monitor", "");
   110       }
   111       folder.RegisterTaskDefinition("Startup", definition,
   112         (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
   113         TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
   114     }
   115 
   116     private void DeleteSchedulerTask() {
   117       ITaskFolder root = scheduler.GetFolder("\\");
   118       try {
   119         ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
   120         folder.DeleteTask("Startup", 0);
   121       } catch (IOException) { }
   122       try {
   123         root.DeleteFolder("Open Hardware Monitor", 0);
   124       } catch (IOException) { }
   125     }
   126 
   127     private void CreateRegistryRun() {
   128       RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
   129       key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
   130     }
   131 
   132     private void DeleteRegistryRun() {
   133       RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
   134       key.DeleteValue("OpenHardwareMonitor");
   135     }
   136 
   137     public bool Startup {
   138       get {
   139         return startup;
   140       }
   141       set {
   142         if (startup != value) {
   143           startup = value;
   144           if (scheduler != null) {
   145             if (startup)
   146               CreateSchedulerTask();
   147             else
   148               DeleteSchedulerTask();
   149           } else {
   150             if (startup)
   151               CreateRegistryRun();
   152             else
   153               DeleteRegistryRun();
   154           }
   155         }
   156       }
   157     }
   158   }
   159 
   160  
   161 
   162  
   163 
   164 }