예제 #1
0
        public static object GetPolicySetting(string registryInformation)
        {
            string             valueName;
            GroupPolicySection section;
            string             key = Key(registryInformation, out valueName, out section);

            // Thread must be STA
            object result = null;
            var    t      = new Thread(() =>
            {
                var gpo = new ComputerGroupPolicyObject();
                using (RegistryKey rootRegistryKey = gpo.GetRootRegistryKey(section))
                {
                    // Data can't be null so we can use this value to indicate key must be delete
                    using (RegistryKey subKey = rootRegistryKey.OpenSubKey(key, true))
                    {
                        if (subKey == null)
                        {
                            result = null;
                        }
                        else
                        {
                            result = subKey.GetValue(valueName);
                        }
                    }
                }
            });

            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            t.Join();

            return(result);
        }
예제 #2
0
        public static void SetPolicySetting(string registryInformation, string settingValue, RegistryValueKind registryValueKind)
        {
            string             valueName;
            GroupPolicySection section;
            string             key = Key(registryInformation, out valueName, out section);

            // Thread must be STA
            Exception exception = null;
            var       t         = new Thread(() =>
            {
                try
                {
                    var gpo = new ComputerGroupPolicyObject();
                    using (RegistryKey rootRegistryKey = gpo.GetRootRegistryKey(section))
                    {
                        // Data can't be null so we can use this value to indicate key must be delete
                        if (settingValue == null)
                        {
                            using (RegistryKey subKey = rootRegistryKey.OpenSubKey(key, true))
                            {
                                if (subKey != null)
                                {
                                    subKey.DeleteValue(valueName);
                                }
                            }
                        }
                        else
                        {
                            using (RegistryKey subKey = rootRegistryKey.CreateSubKey(key))
                            {
                                subKey.SetValue(valueName, settingValue, registryValueKind);
                            }
                        }
                    }

                    gpo.Save();
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
            });

            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            t.Join();

            if (exception != null)
            {
                throw exception;
            }
        }