C # Read and Write the Registry

Source: Internet
Author: User
Net Framework provides two classes in the Microsoft. Win32 namespace to operate the Registry: Registry and registrykey. Both classes are sealed and cannot be inherited. Next we will introduce these two classes respectively. The Registry class provides seven public static domains, representing seven basic primary keys (two of which are not in the XP system): Registry. classesroot corresponds to the hkey_classes_root primary key registry. currentuser corresponds to the HKEY_CURRENT_USER primary key registry. localmachine corresponds to the HKEY_LOCAL_MACHINE primary key registry. the user corresponds to the hkey_user primary key registry. currentconfig corresponds to the primary key registry of heky_current_config. dynda corresponds to the hkey_dyn_data primary key registry. performancedata corresponds to the registrykey class of the hkey_performance_data primary key. Note that the Registry must comply with system permissions; otherwise, an error will be thrown. The method prototype for creating a sub-key is public registrykey createsubkey (string sunbkey). The parameter sunbkey indicates the name or path name of the sub-key to be created. The created sub-key is returned. Otherwise, null is returned. The prototype of the sub-key opening method is public registrykey opensubkey (string name); Public registrykey opensubkey (string name, bool writable); the parameter name indicates the sub-key name to be opened or its path name, the writable parameter indicates whether the opened sub-key can be modified. The sub-key opened in the first method is read-only. The method prototype for deleting a subkey is public void deletesubkey (string subkey). This method is used to delete a specified Primary Key. If the sub-key to be deleted also contains a primary key, the deletion fails and an exception is returned. If you want to completely delete the sub-key under the sub-key's extreme directory, you can use deletesubkeytree, the method is prototype: Public void deletesunkeytree (string subkey); The method prototype for reading key values is as follows: public object getvalue (string name); Public object getvalue (string name, object defaultvalue ); the parameter name indicates the key name. The return type is an object type. If the specified key does not exist, null is returned. If you do not want the returned value to be null, you can specify the defaultvalue parameter. If you specify a parameter, the value specified by this parameter is returned if the read fails. The method prototype for setting the key value is as follows: public object setvalue (string name, object value); the method prototype for deleting the key value is as follows: public void deletevalue (string name); this article comes from the http://blog.csdn.net/greenerycn/archive/2007/03/06/1522616.aspx, reprinted and retained. Thank you! The following is a test example written by myself. There is no running error. 1. write try {registrykey RSG = NULL; If (registry. localmachine. opensubkey ("Software \ ring "). subkeycount <= 0) {registry. localmachine. deletesubkey ("Software \ ring"); registry. localmachine. createsubkey ("Software \ ring");} RSG = registry. localmachine. opensubkey ("Software \ ring", true); // true indicates that the parameter can be modified. // If (RSG. getvalue ("testreg") = NULL) // {// RSG. createsubkey ("testreg"); // else // {// RSG. deletesu Bkey ("testreg"); //} RSG. setvalue ("testreg", system. datetime. now. tostring (); RSG. close ();} catch (exception ex) {This. label2.text = ex. message;} 2. read try {registrykey RSG = NULL; RSG = registry. localmachine. opensubkey ("Software \ ring", true); If (RSG. getvalue ("testreg ")! = NULL) // If the read fails, null {This. label2.text = RSG. getvalue ("testreg"). tostring ();} else this. label2.text = "this key does not exist! "; RSG. Close ();} catch (exception ex) {This. label2.text = ex. Message ;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.