1 //in the. NET managed language C # Operations registry, the main contents include: Creation of registry keys, opening and deletion, creation of key values (setting values, modifications), reading and deleting, determining whether a registry key exists, and determining whether a key value exists.2 //preparatory work:3 //1: To manipulate the registry, we have to introduce the necessary namespaces: using Microsoft.Win32; The namespace contains many registry-related classes, enough for us to use.4 //2: The namespace provides a class: RegistryKey use it to navigate to the beginning of the registry branch: Classesroot,currentuser,users,localmachine,currentconfig ( Represents hkey_classes_root,hkey_current_user,hkey_local_machine,hkey_users,hkey_current_config root key respectively) such as: RegistryKey key = Registry.localmachine;5 //3: In the process of operation involves sub-branches, to use the full path to open deep, a single sub-key name will be error!6 //4: Finally, close () to invoke the RegistryKey object closes the registry modification. 7 //5: Please note that our example below is under the LocalMachine branch.8 //One: Creation of registry keys, opening and deletion9 //1: Create:Ten //Creating a registry key uses the RegistryKey CreateSubKey () method. For example: OneRegistryKey key =Registry.localmachine; ARegistryKey Softwaresubkey = key. CreateSubKey ("software\\mytest");//under HKEY_LOCAL_MACHINE\Software, create a new registry key named MyTest. If it already exists it does not affect!, other paths (out of software) established may not have permission! - //2: Open: - //opening a registry key uses the RegistryKey OpenSubKey () method. For example: the //RegistryKey key = Registry.localmachine; -RegistryKey subkeymytest = key. OpenSubKey ("software\\mytest",true);//Note that the method can also have a Boolean parameter followed by true to indicate that it can be written. - //Note that if the registry key does not exist, this call to this method throws an exception - //3: Delete: + //Deleting a registry key uses the RegistryKey DeleteSubKey () method. For example: - //RegistryKey key = Registry.localmachine; + //key. DeleteSubKey ("Software\\mytest", true); //the method has no return value and can be called directly A //key. Close ();//Note that if the registry key does not exist, this call to this method throws an exception at //Two: Key value creation (set value, modify), read and delete - //1: Create (set value, modify): - //The creation and modification of key values is mainly used in the RegistryKey SetValue () method. - //RegistryKey key = Registry.localmachine; -RegistryKey software = key. OpenSubKey ("software\\mytest",true);//the item ("Software\\mytest") must already exist -Software. SetValue ("Test","Garden");//under Hkey_local_machine\software\mytest, create a key value named "Test" with a value of "garden". If the key value already exists, it will be modified to replace the original key value, if it does not exist, the key value is created. in //Note: SetValue () also has a third parameter, which is primarily used to set the type of the key value, such as: string, Binary, DWORD, and so on, which is the default string. For example: -Software. SetValue ("test2",0, Registryvaluekind.dword);//Binary Information to key. Close (); + //2: Read: - stringinfo; the RegistryKey Keylocalmachine; *Keylocalmachine =Registry.localmachine; $RegistryKey Myreg = Keylocalmachine.opensubkey ("software\\mytest");//or use Myreg = Keylocalmachine.opensubkey ("Software\\mytest", true);Panax Notoginsenginfo = Myreg. GetValue ("Test"). ToString (); -Myreg. Close ();//the info result is: Park the //3: Delete: +RegistryKey Delkey = Registry.LocalMachine.OpenSubKey ("software\\mytest",true); ADelkey.deletevalue ("Test"); theDelkey.close ();//The careful reader may find the second example in which the OpenSubKey () method parameter differs from other examples. + //If you want to change the key value, including create, set, delete the key value and so on to add a Boolean parameter after the method, set to True, indicating writable can be changed, if only read the key value can not add, at this time can write off, you can not write the value (of course, you have to add also can be true)! - //readers also mentioned the problem of reading and writing the default key values, mainly in the settings, read the method of the key name is the default key value operation. $ //such as: $Software. SetValue ("","Garden"); - //in Hkey_local_machine\software\mytest, modify the value of the default key value to "garden". Read similar! - //In addition, the default key value can not be deleted, so do not use the DeleteValue () method to delete, will throw an exception! the //Three: Determine if the registry key exists - //private bool Isregedititemexist ()Wuyi //{ the //string[] subkeynames; - //RegistryKey hklocalmachine = registry.localmachine; Wu //RegistryKey Softwareitem = Hklocalmachine.opensubkey ("Software");//or RegistryKey Softwareitem = Hklocalmachine.opensubkey ("Software", true); - //subkeynames = Softwareitem.getsubkeynames ();//gets the sequence of the names of all the subkeys under the key, and passes them to the predetermined array About //foreach (String keyName in Subkeynames)//traversing the entire array $ //{ - //if (keyName = = "TEST")//determine the name of the subkey - // { - //hklocalmachine.close (); A // //return true; + // } the //} - //hklocalmachine.close (); $ //return false; the //} the //Four: Determine if the key value exists the //private bool Isregeditkeyexit () the //{ - //string[] subkeyvaluenames; in //RegistryKey hklocalmachine = registry.localmachine; the //RegistryKey keyvaluename = Hklocalmachine.opensubkey ("Software\\mytest");//or RegistryKey keyvaluename = hkml. OpenSubKey ("Software\\mytest", true); the //subkeyvaluenames = Keyvaluename.getvaluenames ();//gets the sequence of names of all the key values under that key, and passes them to the predetermined array About //foreach (String kvaluename in Subkeyvaluenames) the //{ the //if (kvaluename = = "Test")//determine the name of the key value the // { + //hklocalmachine.close (); - // //return true; the // }Bayi //} the //hklocalmachine.close (); the //return false; - //} - //Note: The data structure of the Registry the //The registry consists of keys (or "Keys"), sub-keys (subkeys), and value items. the //A key is a folder in a branch, and a subkey is a subfolder in that folder, and the subkey is also a key. the //A value entry is the current definition of a key, consisting of a name, a data type, and an assigned value. the //A key can have one or more values, each with a different name, or the default value for a value if it is empty. - //Data Type the //the Registry has the following four types of data: the //Display Type (in editor) data type description the //REG_SZ string literal string94 //REG_MULTI_SZ string with multiple literal values the //reg_binary binary number binary value, shown in hexadecimal. the //REG_DWORD Double Word a 32-bit binary value that is displayed as a 8-bit hexadecimal value.
[no.000005] C # Registry operations, create, delete, modify, determine whether a node exists