In the Group Policy editing application

Group Policy Editor is a popular way to personalize the system. It does not provide a backup means to lead us in new ones machine or reinstall the system, the need for manual reconfiguration, if the modified policy more time is a troublesome thing. On weekends he studied editing at how to write their own procedures to achieve group policies.

Group Policy startup mode is "gpedit.msc", but he is actually the process is mmc.exe, we can modify the method to monitor their ProcessMonotor registry, specifically refer to this article:

How to view a Group Policy object to modify registry settings

Later found a more compact and easy to use program: RegFromApp , it can be more easily and quickly mmc.exe changes to the registry. A basic example is as follows.

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects\{48914450-7595-411C-AFD3-AE2A07C8500C}User\Software\Policies\Microsoft\PreviousVersions]
"DisableRemotePage"=dword:00000001

 However, actual use of that direct construction of such a registry key is not enough, because it is dynamic path.

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects\{48914450-7595-411C-AFD3-AE2A07C8500C}User\Software\Policies\Microsoft\PreviousVersions]

Section which marked red is dynamic, every time when the Group Policy Editor is not the same. There are also posts online to discuss the issue: https://bbs.csdn.net/topics/70402935

 

To open this dynamic registry key, you need to use IGroupPolicyObject Windows API-related, the Internet also has relevant examples: http://delphi.longzu.net/viewthread.php?tid=49579&extra=page%3D2 .

There are many c # version of the package. I get here is an open source project on GitHub priv10 package, it is encapsulated inside the project LocalPolicy Group Policy interface, a simple example is as follows:

var po  = new ComputerGroupPolicyObject();
var key = po.GetRootRegistryKey(GroupPolicySection.User)
            .OpenSubKey(@"Software\Policies\Microsoft\PreviousVersions");
key.SetValue("DisableRemotePage", 0);|
po.Save();

The above example demonstrates how to open this dynamic registry, then that is the ordinary operation of the registry. After the operation is complete, you need to call the Save writing strategy.

Also, note that the Group Policy-related API is required to use at STA thread, if the MTA is our main program, you can create a new STA thread in the implementation of the relevant API STA thread:

Thread thread = new Thread(EditPolicy);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

Full Off can see Code Example: https://gitee.com/tianfang/GroupPolicyEditor

Guess you like

Origin www.cnblogs.com/TianFang/p/10990888.html