C # class for operating the system registry

Source: Internet
Author: User
Using system;
Using system. Collections. Generic;
Using system. text;
Using Microsoft. Win32; // perform registry operations
Using system. collections; // use arraylist
Using system. Security. cryptography; // encryption and decryption
Using system. IO; // File Operations
Using system. runtime. interopservices; // Call DLL dllimport
Using system. Management; // obtain hardware information
Using system. Net; // used to obtain the IP address
Using system. Drawing; // Image
Using system. net. networkinformation; // used for Ping
Using system. Text. regularexpressions; // regular
Using system. Data;
Using system. Data. sqlclient;
Using Microsoft. VisualBasic; // used in simplified to traditional Chinese
Using system. Web; // html urlencode

// Registry operation
Public class gf_regreadwrite
{

/// <Summary>
/// The read path is keypath and the key name is the key value of the registry. Def is returned by default.
/// </Summary>
/// <Param name = "rootkey"> </param>
/// <Param name = "keypath"> path </param>
/// <Param name = "keyname"> key name </param>
/// <Param name = "RTN"> default value: NULL </param>
/// <Returns> </returns>
Static public bool getregval (registrykey rootkey, string keypath, string keyname, out string RTN)
{
RTN = "";
Try
{
Registrykey key = rootkey. opensubkey (keypath );
RTN = key. getvalue (keyname). tostring ();
Key. Close ();
Return true;
}
Catch
{
Return false;
}
}

/// <Summary>
/// Set the path to keypath and the registry key with the key name keyname to keyval.
/// </Summary>
/// <Param name = "rootkey"> </param>
/// <Param name = "keypath"> </param>
/// <Param name = "keyname"> </param>
/// <Param name = "keyval"> </param>
/// <Returns> </returns>
Static public bool setregval (registrykey rootkey, string keypath, string keyname, string keyval)
{
Try
{
Registrykey key = rootkey. opensubkey (keypath, true );
If (Key = NULL)
Key = rootkey. createsubkey (keypath );
Key. setvalue (keyname, (object) keyval );
Key. Close ();
Return true;
}
Catch
{
Return false;
}
}

/// Key whose path is keypath
Private registrykey createregkey (registrykey rootkey, string keypath)
{
Try
{
Return rootkey. createsubkey (keypath );
}
Catch
{
Return NULL;
}
}
/// Delete the subitem whose path is keypath
Private bool delregsubkey (registrykey rootkey, string keypath)
{
Try
{
Rootkey. deletesubkey (keypath );
Return true;
}
Catch
{
Return false;
}
}
/// Delete the subitem whose path is keypath and its subitem
Private bool delregsubkeytree (registrykey rootkey, string keypath)
{
Try
{
Rootkey. deletesubkeytree (keypath );
Return true;
}
Catch
{
Return false;
}
}
/// Delete the key value with the key name keyname in the keypath
Private bool delregkeyval (registrykey rootkey, string keypath, string keyname)
{
Try
{
Registrykey key = rootkey. opensubkey (keypath, true );
Key. deletevalue (keyname );
Return true;
}
Catch
{
Return false;
}
}
}

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.