GUI/StartupManager.cs
changeset 123 912a06e2bd53
parent 120 41dc766e28b8
child 129 efb1b414d33e
     1.1 --- a/GUI/StartupManager.cs	Thu May 20 21:23:54 2010 +0000
     1.2 +++ b/GUI/StartupManager.cs	Fri May 21 17:28:52 2010 +0000
     1.3 @@ -38,6 +38,7 @@
     1.4  using System;
     1.5  using System.Collections.Generic;
     1.6  using System.IO;
     1.7 +using System.Security.Principal;
     1.8  using System.Windows.Forms;
     1.9  using Microsoft.Win32;
    1.10  using OpenHardwareMonitor.TaskScheduler;
    1.11 @@ -51,25 +52,39 @@
    1.12      private const string REGISTRY_RUN =
    1.13        @"Software\Microsoft\Windows\CurrentVersion\Run";
    1.14  
    1.15 +    private bool IsAdministrator() {
    1.16 +      try {
    1.17 +        WindowsIdentity identity = WindowsIdentity.GetCurrent();
    1.18 +        WindowsPrincipal principal = new WindowsPrincipal(identity);
    1.19 +        return principal.IsInRole(WindowsBuiltInRole.Administrator);
    1.20 +      } catch {
    1.21 +        return false;
    1.22 +      }
    1.23 +    }
    1.24 +
    1.25      public StartupManager() {
    1.26 -      try {
    1.27 -        scheduler = new TaskSchedulerClass();
    1.28 -        scheduler.Connect(null, null, null, null);
    1.29 -      } catch {
    1.30 +      if (IsAdministrator()) {
    1.31 +        try {
    1.32 +          scheduler = new TaskSchedulerClass();
    1.33 +          scheduler.Connect(null, null, null, null);
    1.34 +        } catch {
    1.35 +          scheduler = null;
    1.36 +        }
    1.37 +
    1.38 +        if (scheduler != null) {
    1.39 +          try {
    1.40 +            ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
    1.41 +            IRegisteredTask task = folder.GetTask("Startup");
    1.42 +            startup = task != null;
    1.43 +          } catch (IOException) {
    1.44 +            startup = false;
    1.45 +          } catch (UnauthorizedAccessException) {
    1.46 +            scheduler = null;
    1.47 +          }
    1.48 +        } 
    1.49 +      } else {
    1.50          scheduler = null;
    1.51        }
    1.52 -
    1.53 -      if (scheduler != null) {
    1.54 -        try {
    1.55 -          ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
    1.56 -          IRegisteredTask task = folder.GetTask("Startup");
    1.57 -          startup = task != null;
    1.58 -        } catch (IOException) {
    1.59 -          startup = false;
    1.60 -        } catch (UnauthorizedAccessException) {
    1.61 -          scheduler = null;
    1.62 -        }
    1.63 -      } 
    1.64              
    1.65        if (scheduler == null) {
    1.66          RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN);