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\\pharmac Yinst "; <summary>//Create registry key///</summary>//<param name= "Registryitempath" ></par am> public static void Createregistry (String registryitempath = Registry_item_path) {REGISTRY Key key = Registry.localmachine; RegistryKey software = key. CreateSubKey (Registryitempath); Create a new registry key named Test under HKEY_LOCAL_MACHINE\Software. It does not affect if it already exists! }//<summary>//Get the registry value///</summary>//<returns></returns> public static string GetRegistryValue (string key, String registryitempath = Registry_item_path) {str ing info = ""; RegistryKey Key; Key = Registry.localmachine; var Myreg = Key.opensubkey (Registryitempath); info = Myreg. GetValue (Key). ToString (); Myreg. Close (); return info; }///<summary>//Modify registry key values///</summary>//<param Name= "subkey" ></par am>//<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); The item must already exist software. SetValue (subkey, Val); Under HKEY_LOCAL_MACHINE\Software\Test, create a key value named "Test" with a value of "blog Park". If the key value already exists, it will be modified to replace the original key value, and if it does not exist, the key value is created. Note: SetValue () also has a third parameter, mainly for setting the type of the key value, such as: string, Binary, DWORD, etc. ~ ~ The default is the string. such as://software. SetValue ("Test", "0", Registryvaluekind.dWord); Binary information key. Close (); }}///<summary>//Delete registry key///</summary> public static void Deleteregist Ry (String registryitempath = Registry_item_path) {RegistryKey key = Registry.localmachine; Key. DeleteSubKey (Registryitempath, true); The method has no return value and can be called directly by key. Close (); }///<summary>//Delete registry value///</summary> public static void Deleteregistryvalue (s Tring subkey, String registryitempath = Registry_item_path) {RegistryKey Delkey = Registry.localmachine . OpenSubKey (Registryitempath, true); Delkey.deletevalue (subkey); Delkey.close (); }//<summary>//Determine if 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 (); Gets the sequence of the names of all subkeys under the key and passes them to the predetermined array in the foreach (String keyName in Subkeynames)//traversal entire array { if (KeyName = = "Test")//determine the name of the subkey {HKML. Close (); return true; }} hkml. Close (); return false; }//<summary>//Determine if 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 (); Gets the sequence of names of all the key values under that key, and passes to the predetermined array of foreach (String keyName in Subkeynames) {if (KeyName = = subkey)//Determine the name of the key value {hkml. Close (); return true; }} hkml. Close (); return false; } }}
C # Operations Registry WindowsRegistry