How to apply Windows group policy using .NET?

后端 未结 2 1213
广开言路
广开言路 2020-12-03 02:28

Is it possible to apply (and remove) Windows group policy settings using .NET?

I am working on an application that needs to temporarily put a machine into

2条回答
  •  没有蜡笔的小新
    2020-12-03 02:38

    NOTE: I use two GroupPolicy assembly references: C:\Windows\assembly\GAC_MSIL\Microsoft.GroupPolicy.Management\2.0.0.0__31bf3856ad364e35\Microsoft.GroupPolicy.Management.dll and C:\Windows\assembly\GAC_32\Microsoft.GroupPolicy.Management.Interop\2.0.0.0__31bf3856ad364e35\Microsoft.GroupPolicy.Management.Interop.dll This framework 2.0, so there are mixed code, and you must use app.config: http://msmvps.com/blogs/rfennell/archive/2010/03/27/mixed-mode-assembly-is-built-against-version-v2-0-50727-error-using-net-4-development-web-server.aspx

    I made it like that.

    using System.Collections.ObjectModel;
    using Microsoft.GroupPolicy;
    using Microsoft.Win32;
    
    /// 
    /// Change user's registry policy
    /// 
    /// The name of Group Policy Object(DisplayName)
    /// Is KeyPath(like string path=@"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer")
    /// DWord, ExpandString,... e.t.c 
    /// Name of parameter
    /// Value
    /// result: true\false
    public bool ChangePolicyUser(string gpoName, string keyPath, RegistryValueKind typeOfKey, string parameterName, object value)
        {
            try
            {
                RegistrySetting newSetting = new PolicyRegistrySetting();
                newSetting.Hive = RegistryHive.CurrentUser;
                newSetting.KeyPath = keyPath;
                bool contains = false;
                //newSetting.SetValue(parameterName, value, typeOfKey);
                switch (typeOfKey)
                {
                    case RegistryValueKind.String:
                        newSetting.SetValue(parameterName, (string)value, typeOfKey);
                        break;
                    case RegistryValueKind.ExpandString:
                        newSetting.SetValue(parameterName, (string)value, typeOfKey);
                        break;
                    case RegistryValueKind.DWord:
                        newSetting.SetValue(parameterName, (Int32)value);
                        break;
                    case RegistryValueKind.QWord:
                        newSetting.SetValue(parameterName, (Int64)value);
                        break;
                    case RegistryValueKind.Binary:
                        newSetting.SetValue(parameterName, (byte[])value);
                        break;
                    case RegistryValueKind.MultiString:
                        newSetting.SetValue(parameterName, (string[])value, typeOfKey);
                        break;
                }
                Gpo gpoTarget = _gpDomain.GetGpo(gpoName);
                RegistryPolicy registry = gpoTarget.User.Policy.GetRegistry(false);
                try
                {
                    ReadOnlyCollection items = gpoTarget.User.Policy.GetRegistry(false).Read(newSetting.Hive, keyPath);
                    foreach (RegistryItem item in items)
                    {
                        if (((RegistrySetting) item).ValueName == parameterName)
                        {
                            contains = true;
                        }
                    }
                    registry.Write((PolicyRegistrySetting) newSetting, !contains);
                    registry.Save(false);
                    return true;
                }
                catch (ArgumentException)
                {
                    registry.Write((PolicyRegistrySetting)newSetting, contains);
                    registry.Save(true);
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
    

提交回复
热议问题