소스 검색

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()