Question

I am trying to change some windows settings by group policy settings using C#.
My application creates sub-keys in the group policy objects section successfully, But some times they doesn't work.
For example i am trying to disable desktop using Group policy, I take these steps :

  1. I run Process monitor and configure it to show me the registry changes relating to mmc.exe
  2. Then run gpedit.msc and navigate to the desired option and change it
  3. I copy the registry change which is shown in Process monitor and use it in my app like this :

    mmc.exe  RegSetValue HKCU\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects\{FD0F8A58-1909-410F-8860-4CFF7766FA89}User\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoDesktop        SUCCESS  Type: REG_DWORD, Length: 4, Data: 1
    

And use it like this :

string regPath = @"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer";
string option = "NoDesktop";

SetGroupPolicySetting(regPath, option, 1);

SetGroupPolicySetting uses a dll which can be downloaded from here and is written like this :

private void SetGroupPolicySetting(string registryKeyPath,
                                    string option,
                                    int value ,
                                    GroupPolicySection groupPolicySection = GroupPolicySection.User)
{
    var gpo = new ComputerGroupPolicyObject();
    RegistryKey registryKey = gpo.GetRootRegistryKey(groupPolicySection);
    registryKey.CreateSubKey(registryKeyPath).SetValue(option, value , RegistryValueKind.DWord);
    gpo.Save();
}

After that , there are two subkeys created which are:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects\{27D2FEFF-E5C6-4D8B-B657-0D1E1F2E4BAE}Machine

and

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects\{27D2FEFF-E5C6-4D8B-B657-0D1E1F2E4BAE}User

and finally the NoDesktop option in Explorer section is created in this address :

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects\{27D2FEFF-E5C6-4D8B-B657-0D1E1F2E4BAE}User\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer

though still its not working ! Whats wrong ? its driving me insane!

Was it helpful?

Solution

Setting a GPO alone does not make it active. GPOs usually get reapplied at boot or after a certain period of time. You can probably run something like gpupdate /force (as Administrator) to force reapplying GPOs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top