GUI/StartupManager.cs
changeset 142 48e487749709
parent 139 9611b4d9d898
child 185 edb59f3745e8
     1.1 --- a/GUI/StartupManager.cs	Sun Jun 27 10:54:19 2010 +0000
     1.2 +++ b/GUI/StartupManager.cs	Sun Jun 27 13:02:02 2010 +0000
     1.3 @@ -39,6 +39,7 @@
     1.4  using System.Collections.Generic;
     1.5  using System.IO;
     1.6  using System.Runtime.InteropServices;
     1.7 +using System.Security;
     1.8  using System.Security.Principal;
     1.9  using System.Windows.Forms;
    1.10  using Microsoft.Win32;
    1.11 @@ -49,6 +50,7 @@
    1.12  
    1.13      private TaskSchedulerClass scheduler;
    1.14      private bool startup;
    1.15 +    private bool isAvailable;
    1.16  
    1.17      private const string REGISTRY_RUN =
    1.18        @"Software\Microsoft\Windows\CurrentVersion\Run";
    1.19 @@ -64,6 +66,13 @@
    1.20      }
    1.21  
    1.22      public StartupManager() {
    1.23 +      int p = (int)System.Environment.OSVersion.Platform;
    1.24 +      if ((p == 4) || (p == 128)) {
    1.25 +        scheduler = null;        
    1.26 +        isAvailable = false;
    1.27 +        return;
    1.28 +      }
    1.29 +
    1.30        if (IsAdministrator()) {
    1.31          try {
    1.32            scheduler = new TaskSchedulerClass();
    1.33 @@ -101,15 +110,24 @@
    1.34        } else {
    1.35          scheduler = null;
    1.36        }
    1.37 -            
    1.38 +
    1.39        if (scheduler == null) {
    1.40 -        RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN);
    1.41 -        startup = false;
    1.42 -        if (key != null) {
    1.43 -          string value = (string)key.GetValue("OpenHardwareMonitor");
    1.44 -          if (value != null)
    1.45 -            startup = value == Application.ExecutablePath;
    1.46 +        try {
    1.47 +          using (RegistryKey key =
    1.48 +            Registry.CurrentUser.OpenSubKey(REGISTRY_RUN)) {
    1.49 +            startup = false;
    1.50 +            if (key != null) {
    1.51 +              string value = (string)key.GetValue("OpenHardwareMonitor");
    1.52 +              if (value != null)
    1.53 +                startup = value == Application.ExecutablePath;
    1.54 +            }            
    1.55 +          }
    1.56 +          isAvailable = true;
    1.57 +        } catch (SecurityException) {
    1.58 +          isAvailable = false;
    1.59          }
    1.60 +      } else {
    1.61 +        isAvailable = true;
    1.62        }
    1.63      }
    1.64  
    1.65 @@ -165,6 +183,10 @@
    1.66        key.DeleteValue("OpenHardwareMonitor");
    1.67      }
    1.68  
    1.69 +    public bool IsAvailable {
    1.70 +      get { return isAvailable; }
    1.71 +    }
    1.72 +
    1.73      public bool Startup {
    1.74        get {
    1.75          return startup;
    1.76 @@ -172,24 +194,22 @@
    1.77        set {
    1.78          if (startup != value) {
    1.79            startup = value;
    1.80 -          if (scheduler != null) {
    1.81 -            if (startup)
    1.82 -              CreateSchedulerTask();
    1.83 -            else
    1.84 -              DeleteSchedulerTask();
    1.85 -          } else {
    1.86 -            if (startup)
    1.87 -              CreateRegistryRun();
    1.88 -            else
    1.89 -              DeleteRegistryRun();
    1.90 +          if (isAvailable) {
    1.91 +            if (scheduler != null) {
    1.92 +              if (startup)
    1.93 +                CreateSchedulerTask();
    1.94 +              else
    1.95 +                DeleteSchedulerTask();
    1.96 +            } else {
    1.97 +              if (startup)
    1.98 +                CreateRegistryRun();
    1.99 +              else
   1.100 +                DeleteRegistryRun();
   1.101 +            }
   1.102            }
   1.103          }
   1.104        }
   1.105      }
   1.106    }
   1.107  
   1.108 - 
   1.109 -
   1.110 - 
   1.111 -
   1.112  }