C # Read and Write the Registry Code

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. User corresponds to the hkey_user primary key
Registry. currentconfig corresponds to the primary key of heky_current_config
Registry. dynda corresponds to the hkey_dyn_data primary key
Registry. performancedata corresponds to the hkey_performance_data primary key.
Registrykey class provides methods for registry operations. 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 sunbkey parameter indicates the name or path name of the child key to be created. The created sub-key is returned. Otherwise, null is returned.
The prototype of the sub-key is as follows:
Public registrykey opensubkey (string name );
Public registrykey opensubkey (string name, bool writable );
The name parameter indicates the name or path name of the subkey to be opened. The writable parameter indicates whether the subkey to be opened can be modified. The subkey opened in the first method is read-only.
The method prototype for deleting a sub-key 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 as follows:
Public void deletesunkeytree (string subkey );
The following is a prototype of the key value reading method:
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 following is a prototype for setting the key value:
Public object setvalue (string name, object value );
The method prototype for deleting a key value is as follows:
Public void deletevalue (string name );
This article comes from http://blog.csdn.net/greenerycn/archive/2007/03/06/1522616.aspx, reprinted please keep, 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 key can be modified.
// If (RSG. getvalue ("testreg") = NULL)
//{
// RSG. createsubkey ("testreg ");
//}
// Else
//{
// RSG. deletesubkey ("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 is returned.
{
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;
} Address: http://www.cnblogs.com/ringwang/archive/2008/03/06/1093113.html

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. User corresponds to the hkey_user primary key
Registry. currentconfig corresponds to the primary key of heky_current_config
Registry. dynda corresponds to the hkey_dyn_data primary key
Registry. performancedata corresponds to the hkey_performance_data primary key.
Registrykey class provides methods for registry operations. 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 sunbkey parameter indicates the name or path name of the child key to be created. The created sub-key is returned. Otherwise, null is returned.
The prototype of the sub-key is as follows:
Public registrykey opensubkey (string name );
Public registrykey opensubkey (string name, bool writable );
The name parameter indicates the name or path name of the subkey to be opened. The writable parameter indicates whether the subkey to be opened can be modified. The subkey opened in the first method is read-only.
The method prototype for deleting a sub-key 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 as follows:
Public void deletesunkeytree (string subkey );
The following is a prototype of the key value reading method:
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 following is a prototype for setting the key value:
Public object setvalue (string name, object value );
The method prototype for deleting a key value is as follows:
Public void deletevalue (string name );
This article comes from http://blog.csdn.net/greenerycn/archive/2007/03/06/1522616.aspx, reprinted please keep, 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 key can be modified.
// If (RSG. getvalue ("testreg") = NULL)
//{
// RSG. createsubkey ("testreg ");
//}
// Else
//{
// RSG. deletesubkey ("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 is returned.
{
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.