C # operate the Registry Windows Registry,
Using System; using System. collections. generic; using System. linq; using System. text; using Microsoft. win32; namespace BugsBox. application. core {public class WindowsRegistry {const string REGISTRY_ITEM_PATH = "software \ pharmacyinst "; /// <summary> /// create a registry entry /// </summary> /// <param name = "registryItemPath"> </param> public static void CreateRegistry (string registryItemPath = REGISTRY_ITEM_PATH) {Regi StryKey key = Registry. LocalMachine; RegistryKey software = key. CreateSubKey (registryItemPath); // create a Registry named test under HKEY_LOCAL_MACHINE \ SOFTWARE. If it already exists, it will not be affected!} /// <Summary> /// obtain the registry value /// </summary> /// <returns> </returns> public static string GetRegistryValue (string key, string registryItemPath = REGISTRY_ITEM_PATH) {string info = ""; RegistryKey Key; Key = Registry. localMachine; var myreg = Key. openSubKey (registryItemPath); info = myreg. getValue (key ). toString (); myreg. close (); return info ;}/// <summary> /// modify the registry key value /// </summary> /// <param name = "SubKey"> </param> // <param name = "val"> </param> // <param name = "registryItemPath"> </param> public static void SetRegistryValue (string subKey, string val, string registryItemPath = REGISTRY_ITEM_PATH) {using (RegistryKey key = Registry. localMachine) {RegistryKey software = key. openSubKey (registryItemPath, true); // This item must already exist in software. setValue (subKey, val); // create a name under HKEY_LOCAL_MACHINE \ SOFTWARE \ test It is "test" and the value is the key value of "blog Garden. If the key value already exists, it is changed to replace the original key value. If it does not exist, it is created. // Note: SetValue () has a third parameter, which is mainly used to set the type of the key value, such as string, binary, Dword, and so on ~~ The default value is a string. For example: // software. setValue ("test", "0", RegistryValueKind. DWord); // binary information key. close () ;}/// <summary> // Delete the registry key /// </summary> public static void DeleteRegistry (string registryItemPath = REGISTRY_ITEM_PATH) {RegistryKey key = Registry. localMachine; key. deleteSubKey (registryItemPath, true); // This method has no return value. You can directly call the key. close () ;}/// <summary> /// Delete the registry value /// </summary> public static void DeleteRegistryValue (string subKey, string registryItemPath = REGISTRY_ITEM_PATH) {RegistryKey delKey = Registry. localMachine. openSubKey (registryItemPath, true); delKey. deleteValue (subKey); delKey. close ();} /// <summary> /// determine whether the registry key exists /// </summary> /// <returns> </returns> public static bool IsRegeditItemExist () {string [] subkeyNames; RegistryKey hkml = Registry. localMachine; RegistryKey software = hkml. openSubKey ("SOFTWARE"); // RegistryKey software = hkml. openSubKey ("SOFTWARE", true); subkeyNames = software. getSubKeyNames (); // obtain the sequence of names of all subitem under the item and pass it to foreach (string keyName in subkeyNames) in the predetermined array) // traverse the entire array {if (keyName = "test") // judge the subitem name {hkml. close (); return true ;}} hkml. close (); return false ;} /// <summary> /// determine whether the key value exists /// </summary> /// <returns> </returns> public static bool IsRegeditKeyExist (string subKey, string registryItemPath = REGISTRY_ITEM_PATH) {string [] subkeyNames; RegistryKey hkml = Registry. localMachine; RegistryKey software = hkml. openSubKey (registryItemPath); // RegistryKey software = hkml. openSubKey ("SOFTWARE \ test", true); subkeyNames = software. getValueNames (); // obtain the sequence of names of all key values under the item and pass it to foreach (string keyName in subkeyNames) {if (keyName = subKey) in the predetermined Array) // judge the name of the key value {hkml. close (); return true ;}} hkml. close (); return false ;}}}