Fixed an UnauthorizedAccessException when modifying the auto-startup registry entry.
authormoel.mich
Tue, 14 Sep 2010 19:23:11 +0000
changeset 185edb59f3745e8
parent 184 3ad822f418cb
child 186 010d719f9245
Fixed an UnauthorizedAccessException when modifying the auto-startup registry entry.
GUI/MainForm.cs
GUI/StartupManager.cs
     1.1 --- a/GUI/MainForm.cs	Mon Sep 13 22:55:25 2010 +0000
     1.2 +++ b/GUI/MainForm.cs	Tue Sep 14 19:23:11 2010 +0000
     1.3 @@ -198,7 +198,13 @@
     1.4  
     1.5        autoStart = new UserOption(null, startupManager.Startup, startupMenuItem, settings);
     1.6        autoStart.Changed += delegate(object sender, EventArgs e) {
     1.7 -        startupManager.Startup = autoStart.Value; 
     1.8 +        try {
     1.9 +          startupManager.Startup = autoStart.Value;
    1.10 +        } catch (InvalidOperationException) {
    1.11 +          MessageBox.Show("Updating the auto-startup option failed.", "Error", 
    1.12 +            MessageBoxButtons.OK, MessageBoxIcon.Error);
    1.13 +          autoStart.Value = startupManager.Startup;
    1.14 +        }
    1.15        };
    1.16  
    1.17        readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem, settings);
     2.1 --- a/GUI/StartupManager.cs	Mon Sep 13 22:55:25 2010 +0000
     2.2 +++ b/GUI/StartupManager.cs	Tue Sep 14 19:23:11 2010 +0000
     2.3 @@ -174,7 +174,7 @@
     2.4      }
     2.5  
     2.6      private void CreateRegistryRun() {
     2.7 -      RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
     2.8 +      RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);     
     2.9        key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
    2.10      }
    2.11  
    2.12 @@ -192,20 +192,27 @@
    2.13          return startup;
    2.14        }
    2.15        set {
    2.16 -        if (startup != value) {
    2.17 -          startup = value;
    2.18 +        if (startup != value) {          
    2.19            if (isAvailable) {
    2.20              if (scheduler != null) {
    2.21 -              if (startup)
    2.22 +              if (value)
    2.23                  CreateSchedulerTask();
    2.24                else
    2.25                  DeleteSchedulerTask();
    2.26 +              startup = value;
    2.27              } else {
    2.28 -              if (startup)
    2.29 -                CreateRegistryRun();
    2.30 -              else
    2.31 -                DeleteRegistryRun();
    2.32 +              try {
    2.33 +                if (value)
    2.34 +                  CreateRegistryRun();
    2.35 +                else
    2.36 +                  DeleteRegistryRun();
    2.37 +                startup = value;
    2.38 +              } catch (UnauthorizedAccessException) {
    2.39 +                throw new InvalidOperationException();
    2.40 +              }
    2.41              }
    2.42 +          } else {
    2.43 +            throw new InvalidOperationException();
    2.44            }
    2.45          }
    2.46        }