GUI/StartupManager.cs
author moel.mich
Sun, 27 Jun 2010 10:54:19 +0000
changeset 141 a78cc5c55216
parent 129 efb1b414d33e
child 142 48e487749709
permissions -rw-r--r--
Fixed Issue 76.
     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.Runtime.InteropServices;
    42 using System.Security.Principal;
    43 using System.Windows.Forms;
    44 using Microsoft.Win32;
    45 using OpenHardwareMonitor.TaskScheduler;
    46 
    47 namespace OpenHardwareMonitor.GUI {
    48   public class StartupManager {
    49 
    50     private TaskSchedulerClass scheduler;
    51     private bool startup;
    52 
    53     private const string REGISTRY_RUN =
    54       @"Software\Microsoft\Windows\CurrentVersion\Run";
    55 
    56     private bool IsAdministrator() {
    57       try {
    58         WindowsIdentity identity = WindowsIdentity.GetCurrent();
    59         WindowsPrincipal principal = new WindowsPrincipal(identity);
    60         return principal.IsInRole(WindowsBuiltInRole.Administrator);
    61       } catch {
    62         return false;
    63       }
    64     }
    65 
    66     public StartupManager() {
    67       if (IsAdministrator()) {
    68         try {
    69           scheduler = new TaskSchedulerClass();
    70           scheduler.Connect(null, null, null, null);
    71         } catch {
    72           scheduler = null;
    73         }
    74 
    75         if (scheduler != null) {
    76           try {
    77             // check if the taskscheduler is running
    78             IRunningTaskCollection collection = scheduler.GetRunningTasks(0);            
    79 
    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);
    92               
    93           } catch (IOException) {
    94             startup = false;
    95           } catch (UnauthorizedAccessException) {
    96             scheduler = null;
    97           } catch (COMException) {
    98             scheduler = null;
    99           }
   100         } 
   101       } else {
   102         scheduler = null;
   103       }
   104             
   105       if (scheduler == null) {
   106         RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN);
   107         startup = false;
   108         if (key != null) {
   109           string value = (string)key.GetValue("OpenHardwareMonitor");
   110           if (value != null)
   111             startup = value == Application.ExecutablePath;
   112         }
   113       }
   114     }
   115 
   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";
   125 
   126       ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
   127         TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
   128 
   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);
   134 
   135       ITaskFolder root = scheduler.GetFolder("\\");
   136       ITaskFolder folder;
   137       try {
   138         folder = root.GetFolder("Open Hardware Monitor");
   139       } catch (IOException) {
   140         folder = root.CreateFolder("Open Hardware Monitor", "");
   141       }
   142       folder.RegisterTaskDefinition("Startup", definition,
   143         (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
   144         TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
   145     }
   146 
   147     private void DeleteSchedulerTask() {
   148       ITaskFolder root = scheduler.GetFolder("\\");
   149       try {
   150         ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
   151         folder.DeleteTask("Startup", 0);
   152       } catch (IOException) { }
   153       try {
   154         root.DeleteFolder("Open Hardware Monitor", 0);
   155       } catch (IOException) { }
   156     }
   157 
   158     private void CreateRegistryRun() {
   159       RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
   160       key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
   161     }
   162 
   163     private void DeleteRegistryRun() {
   164       RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
   165       key.DeleteValue("OpenHardwareMonitor");
   166     }
   167 
   168     public bool Startup {
   169       get {
   170         return startup;
   171       }
   172       set {
   173         if (startup != value) {
   174           startup = value;
   175           if (scheduler != null) {
   176             if (startup)
   177               CreateSchedulerTask();
   178             else
   179               DeleteSchedulerTask();
   180           } else {
   181             if (startup)
   182               CreateRegistryRun();
   183             else
   184               DeleteRegistryRun();
   185           }
   186         }
   187       }
   188     }
   189   }
   190 
   191  
   192 
   193  
   194 
   195 }