C # Registry SetValue throws UnauthorizedAccessException

Before you try to answer this, do a Google Quick Search. I would like to note that I already. In this case, I have the following method that tries to change the registry key value. The problem I get is that when it does, it throws an UnauthorizedAccessException , although I opened the key as writable . I run Visual Studio as an administrator and even tried to make a small .exe with the manifest file, forcing it to run as an administrator, which would execute the code without any luck. The key already exists; it does not try to enter the CreateKey method. Here is the code block.

Path = "S-1-5-21-1644491937-1078145449-682003330-5490\Software\Microsoft\Windows\CurrentVersion\Policies\System" Key = "DisableTaskMgr" NewValue = 1 public OperationResult ModifyKey() { OperationResult result = new OperationResult(); if (!Path.IsNullOrEmptyTrim()) { if (!Key.IsNullOrEmptyTrim()) { try { var key = Microsoft.Win32.Registry.Users.OpenSubKey(Path, true); if (key != null) { key.SetValue(Key, NewValue); key.Close(); } else { result = CreateKey(); } } catch (Exception ex) { result.SetFail("Error accessing registry", ex); } } else { result.SetFail("Registry key was null"); } } else { result.SetFail("Registry path was null"); } return result; } 

Do I need to manually navigate to the registry tree so that every OpenSubKey call is recorded? I tried this too, threw the same error anyway ...

+8
c # registry unauthorizedaccessexcepti
source share
6 answers

As a last resort to find out what is happening, I created a simple service to test this code, which will work as a local system account. These are the highest privileges that I could come up with to try to run the code. I ran the code with these permissions.

Special thanks to 0 _____ 0 and Charleh for pointing out the antivirus. I checked the logs and it turned out that he was trying to quarantine my changes. I think even this will not stop the system user from making these changes.

Special thanks to Sorceri , as well as for helping me explore so much.

In conclusion, if you have intermittent, extremely strange behavior, check your antivirus and permissions.

+2
source share

in var for your key

 var key = Microsoft.Win32.Registry.Users.OpenSubKey(Path, true); 

change to

 var key = Microsoft.Win32.Registry.Users.OpenSubKey(Path, RegistryKeyPermissionCheck.ReadWriteSubTree); 
+13
source share

One of the possible problems that I see with your code is that the Path variable is set to a string that does not skip the \ characters. How about something like:

 Path = @"S-1-5-21-1644491937-1078145449-682003330-5490\Software\Microsoft\Windows\CurrentVersion\Policies\System"; 
+3
source share

Have you tried setting accessrule and permissions?

  string user = Environment.UserDomainName + "\\" + Environment.UserName RegistryAccessRule rule = new RegistryAccessRule(user, RegistryRights.FullControl, AccessControlType.Allow); RegistrySecurity security = new RegistrySecurity(); security.AddAccessRule(rule); var key = Microsoft.Win32.Registry.Users.OpenSubKey(Path, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl); key.SetAccessControl(security); 
+3
source share

Install only grants in dword. You should open the registry and on the last path to the folder, click on it and install grants, and select "All applications" and "Check general control". I hope to help you.

-one
source share

just Registry.SetValue(sub_key, key, value);

Example:

 Registry.SetValue( @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MyApp", Application.ExecutablePath); 
-one
source share

All Articles