1

I'm getting reported errors from users who are receiving the error "System.Security.SecurityException, Requested registry access is not allowed." when trying to read the registry. I can't think why someone would not have permission to read the registry and I'm unable to reproduce the problem on my Windows 7 PC. Affected users are running .NET 4.0

Here's the C# code I'm using:

var baseReg = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
var key = baseReg.OpenSubKey(RegKey, RegistryKeyPermissionCheck.ReadSubTree);

if (key != null)
{
    var value = key.GetValue("DisableAutoUpdate", 0);
    if (value != null)
    {
        updatesDisabled = Convert.ToBoolean(value);
    }
}

EDIT: I've checked permissions on the registry key concerned for the affected user and standard Users have read permission for that key.

EDIT 2 and SOLUTION: According to the affected user, installing .NET 4.5.2 resolves the problem! I'm not sure why.

Thanks to @nozzleman's answer below this is fixed by forcing it to open the key as read-only. However it's odd that .NET 4.0 as 4.5.2 appear to behave differently.

NickG
  • 9,315
  • 16
  • 75
  • 115
  • UAC is the work of the devil - even if it was with good intentions - Id guess UAC Is at fault - I seem to recall coming across a similar issue - just like normal users couldnt write to c:\ etc. Would be worth creating a dummy user on your PC with UAC on and trying it – BugFinder Jul 31 '15 at 08:37
  • did you try to run this code with elevated privileges (admin) to see if it works ? – Andrei Gavrila Jul 31 '15 at 08:40
  • similar question http://stackoverflow.com/questions/27543321/read-only-access-to-registry-raises-security-exception that was left unanswered – Andrei Gavrila Jul 31 '15 at 08:41
  • there is an overload of the `OpenSubKey(..)`-Method that allows to add a third parameter. You could try passing `RegistryRights.ReadKey` with that one and see if that solves the issue. – nozzleman Jul 31 '15 at 08:45
  • also msdn mentions the the method you are using throws SecurityException if the user does not have the permissions required to read the registry key. https://msdn.microsoft.com/en-us/library/z9f66s0a(v=vs.110).aspx – Andrei Gavrila Jul 31 '15 at 08:48
  • 1
    alternatively, try the other overload accepting 2 parameters like so `baseReg.OpenSubKey(RegKey, false);` (telling to open the subkey readonly), you dont neet to read the whole sub tree in the given szenarion, do you? – nozzleman Jul 31 '15 at 08:55
  • The problem is I can't reproduce the problem even if I log in with a standard (non-admin) user and UAC turned on. So unless I can reproduce it, I'm reluctant to try and fix it blindly as the app is used by hundreds of people. What I'm trying to find out is, is it normal to not be able to read the registry in any situation? Is my PC different somehow? – NickG Jul 31 '15 at 09:11
  • 1
    I previously answered the question "is it normal to not be able to read the registry in any situation". Accordingly to msdn you should have read permissions to open it. But the solution offered by nozzleman worked for this guy: http://bytes.com/topic/visual-basic-net/answers/879667-why-registry-access-denied-system-security-securityexception-mscorlib-dll . In a way this suggests that the OpenSubKey actually opens the key in a default write mode, which contradicts the msdn page. So if possible play with a couple of scenarios locally to see if msdn page is correct or starrysky – Andrei Gavrila Jul 31 '15 at 11:45
  • @nozzleman Great - that fixed it for me. If you repost as answer I'll accept it. – NickG Jul 31 '15 at 13:32
  • most probably installing .NET 4.5.2 made this work since in the previous frameworks the method did not work as expected but rather it requested write access when not specifically demanding read access – Andrei Gavrila Jul 31 '15 at 16:11

1 Answers1

3

There is an overload of the OpenSubKey(..)-Method that allows to add a third parameter. You could try passing RegistryRights.ReadKey with that one and see if that solves the issue.

baseReg.OpenSubKey(
    RegKey, 
    RegistryKeyPermissionCheck.ReadSubTree
    RegistryRights.ReadKey);

Alternatively, try the other overload accepting 2 parameters like so

baseReg.OpenSubKey(RegKey, false); 

This leads to opening the subkey readonly, and you dont neet to read the whole sub tree in the given szenario..

nozzleman
  • 9,529
  • 4
  • 37
  • 58
  • 1
    Thanks - both of your suggestions work. This works too (but is more code than your answer above): `baseReg.OpenSubKey(RegKey, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey);` – NickG Jul 31 '15 at 13:42
  • I'm wondering if "false" is the default and `baseReg.OpenSubKey(RegKey);` might work too, but as I cant repro the problem, I can't easily test. – NickG Jul 31 '15 at 13:45
  • YOU SAVED MY DAY :-) – Martin.Martinsson Dec 30 '16 at 23:22