コード例 #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 void GroupPolicyEdit()
        {
            var          gpo     = new ComputerGroupPolicyObject();
            const string keyPath =
                @"Software\Microsoft\Windows\CurrentVersion\Group Policy Objects\{9FD4B3FF-CE5D-4436-9FF1-F9EC607330D3}User\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1";

            using (RegistryKey machine = gpo.GetRootRegistryKey(GroupPolicySection.Machine))
            {
                using (RegistryKey terminalServicesKey = machine.CreateSubKey(keyPath))
                {
                    try
                    {
                        if (terminalServicesKey != null)
                        {
                            terminalServicesKey.SetValue
                            (
                                "1804",
                                2,
                                RegistryValueKind.DWord);
                        }
                    }
                    catch (Exception)
                    {
                        //
                    }
                }
            }
            gpo.Save();
        }
コード例 #3
0
    public static void SetPolicySetting(string key, string item, object value, RegistryValueKind kind)
    {
        // C# 不像 Python 那样有方便的 AOP,没法用注解来切STA线程,还得缩进一层挺难看的。
        STAExecutor.Run(() =>
        {
            var gpo     = new ComputerGroupPolicyObject();
            var section = Key(key, out string subkey);

            using var root = gpo.GetRootRegistryKey(section);

            // Data can't be null so we can use this value to indicate key must be delete
            if (value == null)
            {
                using var subKey = root.OpenSubKey(subkey, true);
                if (subKey != null)
                {
                    subKey.DeleteValue(item);
                }
            }
            else
            {
                using var subKey = root.CreateSubKey(subkey);
                subKey.SetValue(item, value, kind);
            }

            gpo.Save();
        });
    }
        internal static void Lock()
        {
#if DEBUG
            return;
#endif
            try
            {
                var gpo = new ComputerGroupPolicyObject();
                using (var machine = gpo.GetRootRegistryKey(GroupPolicySection.User))
                {
                    using (
                        var terminalServicesKey =
                            machine.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"))
                    {
                        terminalServicesKey?.SetValue("NoViewOnDrive", 0x03FFFFFF, RegistryValueKind.DWord);
                    }
                    using (
                        var terminalServicesKey =
                            machine.CreateSubKey(
                                @"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer\DisallowRun")
                        )
                    {
                        terminalServicesKey?.SetValue("1", "iexplore.exe", RegistryValueKind.String);
                    }
                }
                gpo.Save();
            }
            catch
            {
            }
        }
コード例 #5
0
 /// <remarks>引用组件来自:https://bitbucket.org/MartinEden/local-policy/overview </remarks>
 private static void DeletePolicyKey(string path)
 {
     var gpo = new ComputerGroupPolicyObject();
     using (var machine = gpo.GetRootRegistryKey(GroupPolicySection.Machine))
     {
         machine.DeleteSubKey(path, false);
     }
     gpo.Save();
 }
コード例 #6
0
        /// <remarks>引用组件来自: https://bitbucket.org/MartinEden/local-policy/overview </remarks>
        private static void DeletePolicyKey(string path)
        {
            var gpo = new ComputerGroupPolicyObject();

            using (var machine = gpo.GetRootRegistryKey(GroupPolicySection.Machine))
            {
                machine.DeleteSubKey(path, false);
            }
            gpo.Save();
        }
コード例 #7
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();
                        // System.Windows.Forms.MessageBox.Show("?????+gpo " + gpo);
                        using (RegistryKey rootRegistryKey = gpo.GetRootRegistryKey(section))
                        {
                            //System.Windows.Forms.MessageBox.Show("?????+rootkey " + rootRegistryKey);
                            // 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;
                }
            }
コード例 #8
0
    public static object GetPolicySetting(string key, string item)
    {
        return(STAExecutor.Run(() =>
        {
            var gpo = new ComputerGroupPolicyObject();
            var section = Key(key, out string subkey);

            using var root = gpo.GetRootRegistryKey(section);
            using var subKey = root.OpenSubKey(subkey, true);
            return subKey?.GetValue(item);
        }));
    }
コード例 #9
0
 /// <remarks>引用组件来自:https://bitbucket.org/MartinEden/local-policy/overview </remarks>
 private static void SetPolicyKey(string path, string name, object value, RegistryValueKind kind)
 {
     var gpo = new ComputerGroupPolicyObject();
     using (var machine = gpo.GetRootRegistryKey(GroupPolicySection.Machine))
     {
         using (var cerKey = machine.CreateSubKey(path))
         {
             if (cerKey != null) cerKey.SetValue(name, value, kind);
         }
     }
     gpo.Save();
 }
コード例 #10
0
        // *** GPO ***

        static public bool TestGPOTweak(string path, string name, object value, bool usrLevel = false)
        {
            try
            {
                var gpo    = new ComputerGroupPolicyObject(new GroupPolicyObjectSettings(true, true)); // read only so it does not fail without admin rights
                var key    = gpo.GetRootRegistryKey(usrLevel ? GroupPolicySection.User : GroupPolicySection.Machine);
                var subKey = key.CreateSubKey(path);
                return(CmpRegistryValue(subKey, name, value));
            }
            catch (Exception err)
            {
                AppLog.Line("Error in {0}: {1}", MiscFunc.GetCurrentMethod(), err.Message);
            }
            return(false);
        }
コード例 #11
0
        /// <remarks>引用组件来自: https://bitbucket.org/MartinEden/local-policy/overview </remarks>
        private static void SetPolicyKey(string path, string name, object value, RegistryValueKind kind)
        {
            var gpo = new ComputerGroupPolicyObject();

            using (var machine = gpo.GetRootRegistryKey(GroupPolicySection.Machine))
            {
                using (var cerKey = machine.CreateSubKey(path))
                {
                    if (cerKey != null)
                    {
                        cerKey.SetValue(name, value, kind);
                    }
                }
            }
            gpo.Save();
        }
コード例 #12
0
 static public bool SetGPOTweak(string path, string name, object value, bool usrLevel = false)
 {
     try
     {
         var gpo    = new ComputerGroupPolicyObject();
         var key    = gpo.GetRootRegistryKey(usrLevel ? GroupPolicySection.User : GroupPolicySection.Machine);
         var subKey = key.CreateSubKey(path);
         SetRegistryValue(subKey, name, value);
         gpo.Save();
         return(true);
     }
     catch (Exception err)
     {
         AppLog.Line("Error in {0}: {1}", MiscFunc.GetCurrentMethod(), err.Message);
     }
     return(false);
 }
コード例 #13
0
ファイル: TweakEngine.cs プロジェクト: Spartinus/priv10
 static public bool UndoGPOTweak(string path, string name, bool usrLevel = false)
 {
     try
     {
         var gpo    = new ComputerGroupPolicyObject();
         var key    = gpo.GetRootRegistryKey(usrLevel ? GroupPolicySection.User : GroupPolicySection.Machine);
         var subKey = key.CreateSubKey(path);
         subKey.DeleteValue(name, false);
         gpo.Save();
         return(true);
     }
     catch (Exception err)
     {
         AppLog.Exception(err);
     }
     return(false);
 }
コード例 #14
0
        public void GroupPolicyEdit()
        {
            var gpo = new ComputerGroupPolicyObject();
            const string keyPath =
                @"Software\Microsoft\Windows\CurrentVersion\Group Policy Objects\{9FD4B3FF-CE5D-4436-9FF1-F9EC607330D3}User\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1";

            using (RegistryKey machine = gpo.GetRootRegistryKey(GroupPolicySection.Machine))
            {
                using (RegistryKey terminalServicesKey = machine.CreateSubKey(keyPath))
                {
                    try
                    {
                        terminalServicesKey.SetValue
                            (
                                "1804",
                                2,
                                RegistryValueKind.DWord);
                    }
                    catch (Exception e)
                    {
                        //
                    }
                }
            }
            gpo.Save();
        }