0

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!

Hossein
  • 24,202
  • 35
  • 119
  • 224
  • Direct poking of the registry has never been a supported way to change GP objects. It's possible the `gpedit.msc` makes this registry change and then does *something else* that you're not capturing (or the other way around) – Damien_The_Unbeliever Apr 28 '14 at 09:06
  • The catch is some settings work this way some time , and the same settings doesnt work at some other times!!! no desktop used to work but now it doesnt!! – Hossein Apr 28 '14 at 09:22
  • But that's what happens when you go down the unsupported route. Windows 9 *could* make writing a 1 to that registry location format your hard drive. I believe that the registry locations are only *documented* so that you can *read* GP information. – Damien_The_Unbeliever Apr 28 '14 at 09:28

1 Answers1

1

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.

Breit
  • 399
  • 5
  • 9