0

I'm trying to make a program that changes the registry key values on remote machines to block/allow users from personalizing their lock-screen images. It seems the key I need to create is at HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Personalizationwith a name of NoChangingLockScreen. I could easily do this with a .reg file and merge any changes into their registry (I planned on creating a windows service to monitor for changes in the file), although it seems I cannot even modify any keys inside the HKEY_LOCAL_MACHINE class. Please note:

  • I am a domain admin across our network, and all remote computers have admin rights
  • This issue does not just occur when modifying a remote PC's keys, but my own as well
  • I've created the RegistryKey object as writable (See below code)
  • It seems I cannot even use the OpenSubKey method, as reading the local_machine path just throws an object exception
  • I've checked the permissions inside the Registry for that specific class and made sure my account had full control
  • I have found very little documentation on other people having permissions issues

    RegistryKey myKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Policies\Microsoft\Windows", true);
    
Zach R
  • 161
  • 1
  • 14

1 Answers1

0

Registry.ClassesRoot is for HKEY_CLASSES_ROOT. You need to use Registry.LocalMachine field like this:

using (var registryKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Policies\Microsoft\Windows", writable: true))
{
    ...
}

Also note, that this is for local registry access. If you wish to open remote registry, you need to use another method:

using (var remoteBaseKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "RemoteMachineName"))
using (var registryKey = remoteBaseKey.OpenSubKey(@"SOFTWARE\Policies\Microsoft\Windows", writable: true))
{
    ...
}
vasily.sib
  • 3,571
  • 2
  • 19
  • 25
  • Sorry, I combined two separate keys I had by accident, I did use `Registry.LocalMachine` in my test code and it still threw that exception when opening the SubKey – Zach R Sep 27 '18 at 02:51
  • can you then add exact exception message and type to your question? – vasily.sib Sep 27 '18 at 02:53
  • I don't have the project with me (It's on my PC at school) but I believe it was an 'object reference not set to an instance of an object' exception. I'll double check and verify in the morning. – Zach R Sep 27 '18 at 02:55
  • @ZachRaudebaugh, oh, then it's a [NullReferenceException](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) - the most frequent question on StackOverflow – vasily.sib Sep 27 '18 at 02:57
  • Why would it return a NullReferenceException if I'm only opening the SubKey at `HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows` when that SubKey exists on all Windows machines? I could understand it returning the exception if I try to open a key I have not yet created, but I can't even open the existing key to modify anything. – Zach R Sep 27 '18 at 03:01
  • well, we need to see your code, that throws an exception. For example, your code might be like this: `var myKey = Registry.LocalMachine.OpenSubKey(settings.BaseKey, true);` and at run time, `settings` object is `null`, so, accessing `BaseKey` property of `null` object will throw `NullReferenceException` – vasily.sib Sep 27 '18 at 03:05
  • The line that is throwing the exception is `RegistryKey LockPersonalizeKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Personalization", true);` and the exception is `System.Security.SecurityException: 'Requested registry access is not allowed.' – Zach R Sep 27 '18 at 12:30
  • 2 things to check: 1st - `System.Security.Principal.WindowsIdentity.GetCurrent()` right before `OpenSubKey` call, to make sure that you are authenticated as expected; 2nd - this may be UAC limitation. Can you try to run VisualStudio _as Administrator_? – vasily.sib Sep 28 '18 at 02:47
  • 1) `WindowsIdentity.GetCurrent()` returns my user account and IsAuthenticated as true 2} I changed app.manifest to always run this winForm app and VS as administrator – Zach R Sep 28 '18 at 13:50