浏览代码

Fix test to see if registry is readable/writable

Fixes #336
Antony Male 8 年之前
父节点
当前提交
967c23fbc3
共有 1 个文件被更改,包括 11 次插入12 次删除
  1. 11 12
      src/SyncTrayzor/Services/AutostartProvider.cs

+ 11 - 12
src/SyncTrayzor/Services/AutostartProvider.cs

@@ -4,6 +4,7 @@ using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
 using System.Security;
 using System.Security;
+using System.Security.Permissions;
 using System.Text.RegularExpressions;
 using System.Text.RegularExpressions;
 
 
 namespace SyncTrayzor.Services
 namespace SyncTrayzor.Services
@@ -33,6 +34,7 @@ namespace SyncTrayzor.Services
     {
     {
         private static readonly Logger logger = LogManager.GetCurrentClassLogger();
         private static readonly Logger logger = LogManager.GetCurrentClassLogger();
         private const string applicationName = "SyncTrayzor";
         private const string applicationName = "SyncTrayzor";
+        private const string runPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
         // Matches 'SyncTrayzor' and 'SyncTrayzor (n)' (where n is a digit)
         // Matches 'SyncTrayzor' and 'SyncTrayzor (n)' (where n is a digit)
         private static readonly Regex keyRegex = new Regex("^" + applicationName + @"(?: \((\d+)\))?$");
         private static readonly Regex keyRegex = new Regex("^" + applicationName + @"(?: \((\d+)\))?$");
         private readonly string keyName;
         private readonly string keyName;
@@ -65,13 +67,10 @@ namespace SyncTrayzor.Services
         {
         {
             try
             try
             {
             {
-                // Apparently just opening the key is not enough - there's an ACL where it will still crash when trying to write
-                // to it
-                using (var key = this.OpenRegistryKey(true))
-                {
-                    var value = key.GetValue(this.keyName);
-                    key.SetValue(this.keyName, value);
-                }
+                this.OpenRegistryKey(true).Dispose();
+
+                // Not sure if the above check is needed now that we have this
+                new RegistryPermission(RegistryPermissionAccess.AllAccess, runPath).Demand();
 
 
                 this._canWrite = true;
                 this._canWrite = true;
                 this._canRead = true;
                 this._canRead = true;
@@ -82,10 +81,10 @@ namespace SyncTrayzor.Services
 
 
             try
             try
             {
             {
-                using (var key = this.OpenRegistryKey(false))
-                {
-                    var value = key.GetValue(this.keyName);
-                }
+                this.OpenRegistryKey(false).Dispose();
+
+                // Not sure if the above check is needed now that we have this
+                new RegistryPermission(RegistryPermissionAccess.Read, runPath).Demand();
 
 
                 this._canRead = true;
                 this._canRead = true;
                 logger.Info("Have read-only access to the registry");
                 logger.Info("Have read-only access to the registry");
@@ -148,7 +147,7 @@ namespace SyncTrayzor.Services
 
 
         private RegistryKey OpenRegistryKey(bool writable)
         private RegistryKey OpenRegistryKey(bool writable)
         {
         {
-            return Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", writable);
+            return Registry.CurrentUser.OpenSubKey(runPath, writable);
         }
         }
 
 
         public AutostartConfiguration GetCurrentSetup()
         public AutostartConfiguration GetCurrentSetup()