瀏覽代碼

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 8 年之前
父節點
當前提交
4f826a8ffb
共有 1 個文件被更改,包括 27 次插入16 次删除
  1. 27 16
      src/SyncTrayzor/Services/AutostartProvider.cs

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

@@ -68,30 +68,40 @@ namespace SyncTrayzor.Services
         {
             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 (UnauthorizedAccessException) { }
 
             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 (UnauthorizedAccessException) { }
 
             logger.Info("Have no access to the registry");
         }
@@ -148,7 +158,8 @@ namespace SyncTrayzor.Services
 
         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()