Explorar o código

Handle cases where autostart register key doesn't exist, and can't be created

We try and create the key if it doesn't already exist. If that fails, we'll
handle that as well.

Fixes #344
Antony Male %!s(int64=8) %!d(string=hai) anos
pai
achega
4f826a8ffb
Modificáronse 1 ficheiros con 27 adicións e 16 borrados
  1. 27 16
      src/SyncTrayzor/Services/AutostartProvider.cs

+ 27 - 16
src/SyncTrayzor/Services/AutostartProvider.cs

@@ -68,30 +68,40 @@ namespace SyncTrayzor.Services
         {
         {
             try
             try
             {
             {
-                this.OpenRegistryKey(true).Dispose();
-
-                // Not sure if the above check is needed now that we have this
-                new RegistryPermission(RegistryPermissionAccess.AllAccess, runPathWithHive).Demand();
+                using (var key = this.OpenRegistryKey(true))
+                {
+                    if (key != null) // It's null if "there was an error"
+                    {
+                        // We can open it, but not have access to create subkeys, I think
+                        new RegistryPermission(RegistryPermissionAccess.AllAccess, runPathWithHive).Demand();
 
 
-                this._canWrite = true;
-                this._canRead = true;
-                logger.Info("Have read/write access to the registry");
-                return;
+                        this._canWrite = true;
+                        this._canRead = true;
+                        logger.Info("Have read/write access to the registry");
+                        return;
+                    }
+                }
             }
             }
             catch (SecurityException) { }
             catch (SecurityException) { }
+            catch (UnauthorizedAccessException) { }
 
 
             try
             try
             {
             {
-                this.OpenRegistryKey(false).Dispose();
-
-                // Not sure if the above check is needed now that we have this
-                new RegistryPermission(RegistryPermissionAccess.Read, runPathWithHive).Demand();
+                using (var key = this.OpenRegistryKey(false))
+                {
+                    if (key != null) // It's null if "there was an error"
+                    {
+                        // We can open it, but not have access to read subkeys, I think
+                        new RegistryPermission(RegistryPermissionAccess.Read, runPathWithHive).Demand();
 
 
-                this._canRead = true;
-                logger.Info("Have read-only access to the registry");
-                return;
+                        this._canRead = true;
+                        logger.Info("Have read-only access to the registry");
+                        return;
+                    }
+                }
             }
             }
             catch (SecurityException) { }
             catch (SecurityException) { }
+            catch (UnauthorizedAccessException) { }
 
 
             logger.Info("Have no access to the registry");
             logger.Info("Have no access to the registry");
         }
         }
@@ -148,7 +158,8 @@ namespace SyncTrayzor.Services
 
 
         private RegistryKey OpenRegistryKey(bool writable)
         private RegistryKey OpenRegistryKey(bool writable)
         {
         {
-            return Registry.CurrentUser.OpenSubKey(runPath, writable);
+            var key = Registry.CurrentUser.CreateSubKey(runPath, writable ? RegistryKeyPermissionCheck.ReadWriteSubTree : RegistryKeyPermissionCheck.ReadSubTree);
+            return key;
         }
         }
 
 
         public AutostartConfiguration GetCurrentSetup()
         public AutostartConfiguration GetCurrentSetup()