GUI/StartupManager.cs
author StephaneLenclud
Thu, 18 Apr 2013 23:25:10 +0200
changeset 402 ded1323b61ee
parent 185 edb59f3745e8
permissions -rw-r--r--
Front View plug-in does not init if no sensor added.
Fixing some format to make strings shorter.
Now trying to start SoundGraphAccess.exe process from same directory.
Packed mode now can display three sensors along with the current time.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.IO;
    14 using System.Runtime.InteropServices;
    15 using System.Security;
    16 using System.Security.Principal;
    17 using System.Windows.Forms;
    18 using Microsoft.Win32;
    19 using OpenHardwareMonitor.TaskScheduler;
    20 
    21 namespace OpenHardwareMonitor.GUI {
    22   public class StartupManager {
    23 
    24     private TaskSchedulerClass scheduler;
    25     private bool startup;
    26     private bool isAvailable;
    27 
    28     private const string REGISTRY_RUN =
    29       @"Software\Microsoft\Windows\CurrentVersion\Run";
    30 
    31     private bool IsAdministrator() {
    32       try {
    33         WindowsIdentity identity = WindowsIdentity.GetCurrent();
    34         WindowsPrincipal principal = new WindowsPrincipal(identity);
    35         return principal.IsInRole(WindowsBuiltInRole.Administrator);
    36       } catch {
    37         return false;
    38       }
    39     }
    40 
    41     public StartupManager() {
    42       int p = (int)System.Environment.OSVersion.Platform;
    43       if ((p == 4) || (p == 128)) {
    44         scheduler = null;        
    45         isAvailable = false;
    46         return;
    47       }
    48 
    49       if (IsAdministrator()) {
    50         try {
    51           scheduler = new TaskSchedulerClass();
    52           scheduler.Connect(null, null, null, null);
    53         } catch {
    54           scheduler = null;
    55         }
    56 
    57         if (scheduler != null) {
    58           try {
    59             // check if the taskscheduler is running
    60             IRunningTaskCollection collection = scheduler.GetRunningTasks(0);            
    61 
    62             ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
    63             IRegisteredTask task = folder.GetTask("Startup");
    64             startup = (task != null) && 
    65               (task.Definition.Triggers.Count > 0) &&
    66               (task.Definition.Triggers[1].Type == 
    67                 TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON) &&
    68               (task.Definition.Actions.Count > 0) &&
    69               (task.Definition.Actions[1].Type ==
    70                 TASK_ACTION_TYPE.TASK_ACTION_EXEC) &&
    71               (task.Definition.Actions[1] as IExecAction != null) &&
    72               ((task.Definition.Actions[1] as IExecAction).Path ==
    73                 Application.ExecutablePath);
    74               
    75           } catch (IOException) {
    76             startup = false;
    77           } catch (UnauthorizedAccessException) {
    78             scheduler = null;
    79           } catch (COMException) {
    80             scheduler = null;
    81           }
    82         } 
    83       } else {
    84         scheduler = null;
    85       }
    86 
    87       if (scheduler == null) {
    88         try {
    89           using (RegistryKey key =
    90             Registry.CurrentUser.OpenSubKey(REGISTRY_RUN)) {
    91             startup = false;
    92             if (key != null) {
    93               string value = (string)key.GetValue("OpenHardwareMonitor");
    94               if (value != null)
    95                 startup = value == Application.ExecutablePath;
    96             }            
    97           }
    98           isAvailable = true;
    99         } catch (SecurityException) {
   100           isAvailable = false;
   101         }
   102       } else {
   103         isAvailable = true;
   104       }
   105     }
   106 
   107     private void CreateSchedulerTask() {
   108       ITaskDefinition definition = scheduler.NewTask(0);
   109       definition.RegistrationInfo.Description =
   110         "This task starts the Open Hardware Monitor on Windows startup.";
   111       definition.Principal.RunLevel =
   112         TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
   113       definition.Settings.DisallowStartIfOnBatteries = false;
   114       definition.Settings.StopIfGoingOnBatteries = false;
   115       definition.Settings.ExecutionTimeLimit = "PT0S";
   116 
   117       ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
   118         TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
   119 
   120       IExecAction action = (IExecAction)definition.Actions.Create(
   121         TASK_ACTION_TYPE.TASK_ACTION_EXEC);
   122       action.Path = Application.ExecutablePath;
   123       action.WorkingDirectory =
   124         Path.GetDirectoryName(Application.ExecutablePath);
   125 
   126       ITaskFolder root = scheduler.GetFolder("\\");
   127       ITaskFolder folder;
   128       try {
   129         folder = root.GetFolder("Open Hardware Monitor");
   130       } catch (IOException) {
   131         folder = root.CreateFolder("Open Hardware Monitor", "");
   132       }
   133       folder.RegisterTaskDefinition("Startup", definition,
   134         (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
   135         TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
   136     }
   137 
   138     private void DeleteSchedulerTask() {
   139       ITaskFolder root = scheduler.GetFolder("\\");
   140       try {
   141         ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
   142         folder.DeleteTask("Startup", 0);
   143       } catch (IOException) { }
   144       try {
   145         root.DeleteFolder("Open Hardware Monitor", 0);
   146       } catch (IOException) { }
   147     }
   148 
   149     private void CreateRegistryRun() {
   150       RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);     
   151       key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
   152     }
   153 
   154     private void DeleteRegistryRun() {
   155       RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
   156       key.DeleteValue("OpenHardwareMonitor");
   157     }
   158 
   159     public bool IsAvailable {
   160       get { return isAvailable; }
   161     }
   162 
   163     public bool Startup {
   164       get {
   165         return startup;
   166       }
   167       set {
   168         if (startup != value) {          
   169           if (isAvailable) {
   170             if (scheduler != null) {
   171               if (value)
   172                 CreateSchedulerTask();
   173               else
   174                 DeleteSchedulerTask();
   175               startup = value;
   176             } else {
   177               try {
   178                 if (value)
   179                   CreateRegistryRun();
   180                 else
   181                   DeleteRegistryRun();
   182                 startup = value;
   183               } catch (UnauthorizedAccessException) {
   184                 throw new InvalidOperationException();
   185               }
   186             }
   187           } else {
   188             throw new InvalidOperationException();
   189           }
   190         }
   191       }
   192     }
   193   }
   194 
   195 }