GUI/StartupManager.cs
author moel.mich
Sat, 29 May 2010 13:49:20 +0000
changeset 129 efb1b414d33e
parent 123 912a06e2bd53
child 139 9611b4d9d898
permissions -rw-r--r--
Fixed Issue 64.
     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.Security.Principal;
    42 using System.Windows.Forms;
    43 using Microsoft.Win32;
    44 using OpenHardwareMonitor.TaskScheduler;
    45 
    46 namespace OpenHardwareMonitor.GUI {
    47   public class StartupManager {
    48 
    49     private TaskSchedulerClass scheduler;
    50     private bool startup;
    51 
    52     private const string REGISTRY_RUN =
    53       @"Software\Microsoft\Windows\CurrentVersion\Run";
    54 
    55     private bool IsAdministrator() {
    56       try {
    57         WindowsIdentity identity = WindowsIdentity.GetCurrent();
    58         WindowsPrincipal principal = new WindowsPrincipal(identity);
    59         return principal.IsInRole(WindowsBuiltInRole.Administrator);
    60       } catch {
    61         return false;
    62       }
    63     }
    64 
    65     public StartupManager() {
    66       if (IsAdministrator()) {
    67         try {
    68           scheduler = new TaskSchedulerClass();
    69           scheduler.Connect(null, null, null, null);
    70         } catch {
    71           scheduler = null;
    72         }
    73 
    74         if (scheduler != null) {
    75           try {
    76             ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
    77             IRegisteredTask task = folder.GetTask("Startup");
    78             startup = (task != null) && 
    79               (task.Definition.Triggers.Count > 0) &&
    80               (task.Definition.Triggers[1].Type == 
    81                 TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON) &&
    82               (task.Definition.Actions.Count > 0) &&
    83               (task.Definition.Actions[1].Type ==
    84                 TASK_ACTION_TYPE.TASK_ACTION_EXEC) &&
    85               (task.Definition.Actions[1] as IExecAction != null) &&
    86               ((task.Definition.Actions[1] as IExecAction).Path ==
    87                 Application.ExecutablePath);
    88               
    89           } catch (IOException) {
    90             startup = false;
    91           } catch (UnauthorizedAccessException) {
    92             scheduler = null;
    93           }
    94         } 
    95       } else {
    96         scheduler = null;
    97       }
    98             
    99       if (scheduler == null) {
   100         RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN);
   101         startup = false;
   102         if (key != null) {
   103           string value = (string)key.GetValue("OpenHardwareMonitor");
   104           if (value != null)
   105             startup = value == Application.ExecutablePath;
   106         }
   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       try {
   132         folder = root.GetFolder("Open Hardware Monitor");
   133       } catch (IOException) {
   134         folder = root.CreateFolder("Open Hardware Monitor", "");
   135       }
   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     }
   151 
   152     private void CreateRegistryRun() {
   153       RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
   154       key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
   155     }
   156 
   157     private void DeleteRegistryRun() {
   158       RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
   159       key.DeleteValue("OpenHardwareMonitor");
   160     }
   161 
   162     public bool Startup {
   163       get {
   164         return startup;
   165       }
   166       set {
   167         if (startup != value) {
   168           startup = value;
   169           if (scheduler != null) {
   170             if (startup)
   171               CreateSchedulerTask();
   172             else
   173               DeleteSchedulerTask();
   174           } else {
   175             if (startup)
   176               CreateRegistryRun();
   177             else
   178               DeleteRegistryRun();
   179           }
   180         }
   181       }
   182     }
   183   }
   184 
   185  
   186 
   187  
   188 
   189 }