简体   繁体   中英

Editing Local_Machine RegistryKeys Programmatically (C#)

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\\Personalization with 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); 

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))
{
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM