Fixed Issue 71.
authormoel.mich
Sun, 27 Jun 2010 13:02:02 +0000
changeset 14248e487749709
parent 141 a78cc5c55216
child 143 d5504ec7b85a
Fixed Issue 71.
GUI/MainForm.cs
GUI/StartupManager.cs
     1.1 --- a/GUI/MainForm.cs	Sun Jun 27 10:54:19 2010 +0000
     1.2 +++ b/GUI/MainForm.cs	Sun Jun 27 13:02:02 2010 +0000
     1.3 @@ -141,11 +141,7 @@
     1.4          UnitManager.TemperatureUnit == TemperatureUnit.Celcius;
     1.5        fahrenheitToolStripMenuItem.Checked = !celciusToolStripMenuItem.Checked;
     1.6  
     1.7 -      // Hide the auto startup menu item on Unix
     1.8 -      int p = (int)System.Environment.OSVersion.Platform;
     1.9 -      if ((p == 4) || (p == 128)) {
    1.10 -        startupMenuItem.Visible = false;
    1.11 -      }
    1.12 +      startupMenuItem.Visible = startupManager.IsAvailable;
    1.13        
    1.14        if (startMinMenuItem.Checked) {
    1.15          if (!minTrayMenuItem.Checked) {
     2.1 --- a/GUI/StartupManager.cs	Sun Jun 27 10:54:19 2010 +0000
     2.2 +++ b/GUI/StartupManager.cs	Sun Jun 27 13:02:02 2010 +0000
     2.3 @@ -39,6 +39,7 @@
     2.4  using System.Collections.Generic;
     2.5  using System.IO;
     2.6  using System.Runtime.InteropServices;
     2.7 +using System.Security;
     2.8  using System.Security.Principal;
     2.9  using System.Windows.Forms;
    2.10  using Microsoft.Win32;
    2.11 @@ -49,6 +50,7 @@
    2.12  
    2.13      private TaskSchedulerClass scheduler;
    2.14      private bool startup;
    2.15 +    private bool isAvailable;
    2.16  
    2.17      private const string REGISTRY_RUN =
    2.18        @"Software\Microsoft\Windows\CurrentVersion\Run";
    2.19 @@ -64,6 +66,13 @@
    2.20      }
    2.21  
    2.22      public StartupManager() {
    2.23 +      int p = (int)System.Environment.OSVersion.Platform;
    2.24 +      if ((p == 4) || (p == 128)) {
    2.25 +        scheduler = null;        
    2.26 +        isAvailable = false;
    2.27 +        return;
    2.28 +      }
    2.29 +
    2.30        if (IsAdministrator()) {
    2.31          try {
    2.32            scheduler = new TaskSchedulerClass();
    2.33 @@ -101,15 +110,24 @@
    2.34        } else {
    2.35          scheduler = null;
    2.36        }
    2.37 -            
    2.38 +
    2.39        if (scheduler == null) {
    2.40 -        RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN);
    2.41 -        startup = false;
    2.42 -        if (key != null) {
    2.43 -          string value = (string)key.GetValue("OpenHardwareMonitor");
    2.44 -          if (value != null)
    2.45 -            startup = value == Application.ExecutablePath;
    2.46 +        try {
    2.47 +          using (RegistryKey key =
    2.48 +            Registry.CurrentUser.OpenSubKey(REGISTRY_RUN)) {
    2.49 +            startup = false;
    2.50 +            if (key != null) {
    2.51 +              string value = (string)key.GetValue("OpenHardwareMonitor");
    2.52 +              if (value != null)
    2.53 +                startup = value == Application.ExecutablePath;
    2.54 +            }            
    2.55 +          }
    2.56 +          isAvailable = true;
    2.57 +        } catch (SecurityException) {
    2.58 +          isAvailable = false;
    2.59          }
    2.60 +      } else {
    2.61 +        isAvailable = true;
    2.62        }
    2.63      }
    2.64  
    2.65 @@ -165,6 +183,10 @@
    2.66        key.DeleteValue("OpenHardwareMonitor");
    2.67      }
    2.68  
    2.69 +    public bool IsAvailable {
    2.70 +      get { return isAvailable; }
    2.71 +    }
    2.72 +
    2.73      public bool Startup {
    2.74        get {
    2.75          return startup;
    2.76 @@ -172,24 +194,22 @@
    2.77        set {
    2.78          if (startup != value) {
    2.79            startup = value;
    2.80 -          if (scheduler != null) {
    2.81 -            if (startup)
    2.82 -              CreateSchedulerTask();
    2.83 -            else
    2.84 -              DeleteSchedulerTask();
    2.85 -          } else {
    2.86 -            if (startup)
    2.87 -              CreateRegistryRun();
    2.88 -            else
    2.89 -              DeleteRegistryRun();
    2.90 +          if (isAvailable) {
    2.91 +            if (scheduler != null) {
    2.92 +              if (startup)
    2.93 +                CreateSchedulerTask();
    2.94 +              else
    2.95 +                DeleteSchedulerTask();
    2.96 +            } else {
    2.97 +              if (startup)
    2.98 +                CreateRegistryRun();
    2.99 +              else
   2.100 +                DeleteRegistryRun();
   2.101 +            }
   2.102            }
   2.103          }
   2.104        }
   2.105      }
   2.106    }
   2.107  
   2.108 - 
   2.109 -
   2.110 - 
   2.111 -
   2.112  }