How to edit the gpedit policy

后端 未结 1 458
孤街浪徒
孤街浪徒 2021-01-23 01:33

I would like to ask how can I edit the gpedit.msc policy using C#?

1条回答
  •  走了就别回头了
    2021-01-23 01:44

    gpedit.msc is merely an interface into the registry settings; there is no API or exposed framework for gpedit. To manipulate a value as gpedit would you need to manipulate the associated RegistryKey.

    class RegKey
    {
        static void Main()
        {
            // Create a subkey named Test9999 under HKEY_CURRENT_USER.
            RegistryKey test9999 = 
                Registry.CurrentUser.CreateSubKey("Test9999");
            // Create two subkeys under HKEY_CURRENT_USER\Test9999. The
            // keys are disposed when execution exits the using statement.
            using(RegistryKey 
                testName = test9999.CreateSubKey("TestName"),
                testSettings = test9999.CreateSubKey("TestSettings"))
            {
                // Create data for the TestSettings subkey.
                testSettings.SetValue("Language", "French");
                testSettings.SetValue("Level", "Intermediate");
                testSettings.SetValue("ID", 123);
            }
    
            // Print the information from the Test9999 subkey.
            Console.WriteLine("There are {0} subkeys under {1}.", 
                test9999.SubKeyCount.ToString(), test9999.Name);
            foreach(string subKeyName in test9999.GetSubKeyNames())
            {
                using(RegistryKey 
                    tempKey = test9999.OpenSubKey(subKeyName))
                {
                    Console.WriteLine("\nThere are {0} values for {1}.", 
                        tempKey.ValueCount.ToString(), tempKey.Name);
                    foreach(string valueName in tempKey.GetValueNames())
                    {
                        Console.WriteLine("{0,-8}: {1}", valueName, 
                            tempKey.GetValue(valueName).ToString());
                    }
                }
            }
    
            using(RegistryKey 
                testSettings = test9999.OpenSubKey("TestSettings", true))
            {
                // Delete the ID value.
                testSettings.DeleteValue("id");
    
                // Verify the deletion.
                Console.WriteLine((string)testSettings.GetValue(
                    "id", "ID not found."));
            }
    
            // Delete or close the new subkey.
            Console.Write("\nDelete newly created registry key? (Y/N) ");
            if(Char.ToUpper(Convert.ToChar(Console.Read())) == 'Y')
            {
                Registry.CurrentUser.DeleteSubKeyTree("Test9999");
                Console.WriteLine("\nRegistry key {0} deleted.", 
                    test9999.Name);
            }
            else
            {
                Console.WriteLine("\nRegistry key {0} closed.", 
                    test9999.ToString());
                test9999.Close();
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题