C # Operations Registry

Source: Internet
Author: User

1. Registry key

A straightforward understanding is the Registry's folder item. The operation of it is nothing more than adding and removing changes, the following are described separately.

(1) Enquiry

/// <summary>        ///determine if a registry key exists/// </summary>        /// <param name= "Opensubkeyname" >registry key Path</param>        /// <param name= "ItemName" >Project Name</param>        /// <returns>exists true, does not exist false</returns>        Private BOOLIsregedititemexist (stringOpensubkeyname,stringitemname, Enumregistrytype type) {            string[] subkeynames; RegistryKey Key= This.            Getregistrytype (type); Try            {                if(string. IsNullOrEmpty (Opensubkeyname)) {Throw NewException ("opensubkeyname is null"); }                if(string. IsNullOrEmpty (ItemName)) {Throw NewException ("itemname is null"); } RegistryKey Software=Key.opensubkey (opensubkeyname); if(software = =NULL)                {                    return false; } subkeynames=Software.getsubkeynames (); foreach(stringKeyNameinchsubkeynames) {                    if(KeyName = =itemname)                        {Key.close (); return true;                }} key.close (); return false; }            Catch(Exception ex) {key.close (); Throw NewException (ex.                Message.tostring ()); return false; }        }
View Code

(2) Increase

 Public voidCreateregedititem (stringOpensubkeyname,stringitemname, Enumregistrytype type) {            //exist, return            if(Isregedititemexist (Opensubkeyname, ItemName, type)) {return; }            //does not exist, it increases the operationRegistryKey Key =Registry.localmachine; RegistryKey Software= Key.createsubkey (Opensubkeyname +"\\"+itemname);            Key.close (); return; }
View Code

(3) Delete

 Public voidDeleteregedititem (stringOpensubkeyname,stringitemname, Enumregistrytype type) {            //does not exist, returns            if(!isregedititemexist (Opensubkeyname, ItemName, type)) {                return; }            //exists, then the delete operationRegistryKey Key = This.            Getregistrytype (type); Key.deletesubkey (Opensubkeyname+"\\"+itemname);            Key.close (); return; }
View Code

2. Registry key value

The straightforward understanding is the value of each property within the registry key. The operation of it is nothing more than adding and removing changes, the following are described separately.

(1) Whether the query exists

/// <summary>        ///determine if the registry key exists/// </summary>        /// <param name= "Opensubkeyname" ></param>        /// <param name= "KeyName" ></param>        /// <returns></returns>        Private BOOLIsregeditkeyexist (stringOpensubkeyname,stringkeyName, Enumregistrytype type) {            string[] subkeynames; RegistryKey Key= This.            Getregistrytype (type); RegistryKey Software=Key.opensubkey (opensubkeyname); if(software = =NULL)            {                return false; } subkeynames=Software.getvaluenames (); foreach(stringNameinchsubkeynames) {                if(Name = =keyName)                    {Key.close (); return true;            }} key.close (); return false; }
View Code

(2) Query value

 Public stringReadregeditkey (stringOpensubkeyname,stringkeyName, Enumregistrytype type) {            if(!isregeditkeyexist (Opensubkeyname, KeyName, type)) {                return string.            Empty; } RegistryKey Key= This.            Getregistrytype (type); RegistryKey MyKey=Key.opensubkey (opensubkeyname); if(MyKey = =NULL)            {                return string.            Empty; }            stringKeyInfo =Mykey.getvalue (keyName).            ToString ();            Key.close (); returnKeyInfo; }
View Code

(3) Modify or add value

 Public voidEditregieditkey (stringOpensubkeyname,stringKeyName,ObjectkeyValue, Enumregistrytype type) {            if(Isregeditkeyexist (Opensubkeyname, KeyName, type)) {return; } RegistryKey Key= This.            Getregistrytype (type); RegistryKey Software= Key.opensubkey (Opensubkeyname,true); if(software = =NULL)            {                return;            } software.setvalue (KeyName, KeyValue);            Key.close (); return; }
View Code

(4) Delete value

 Public voidDeleteregieditkey (stringOpensubkeyname,stringkeyName, Enumregistrytype type) {            if(Isregeditkeyexist (Opensubkeyname, KeyName, type)) {return; } RegistryKey Key= This.            Getregistrytype (type); RegistryKey Software= Key.opensubkey (Opensubkeyname,true); if(software = =NULL)            {                return;            } software.deletevalue (KeyName);            Key.close (); return; }
View Code

3. Auxiliary methods

(1) References

Add the following line of references to invoke the various classes of the Operation registry.

using Microsoft.Win32;

(2) Common methods

Write only a part, not write the whole, for reference only.

/// <summary>        ///         /// </summary>        /// <param name= "type" >Registry Type</param>        /// <returns></returns>        PrivateRegistryKey getregistrytype (enumregistrytype type) {RegistryKey Key; Switch(type) { CaseEnumRegistryType.HKEY_CURRENT_USER:Key=Registry.currentuser;  Break;  CaseEnumRegistryType.HKEY_LOCAL_MACHINE:Key=Registry.localmachine;  Break;  CaseEnumRegistryType.HKEY_USER:Key=registry.users;  Break; default: Key=Registry.localmachine;  Break; }            returnKey; }
View Code

(3) Enum class

Write only a part, not write the whole, for reference only.

 Public enumEnumregistrytype {/// <summary>        ///Current User/// </summary>HKEY_CURRENT_USER,/// <summary>        ///Local Host/// </summary>HKEY_LOCAL_MACHINE,/// <summary>        ///User/// </summary>Hkey_user}
View Code

(4) 32-bit and 64-bit differences

All of the above defaults are based on a 32-bit system, and if it is a 64-bit system, the registry is written to the subkey under "Wow6432node".

The method to determine if 64 is the system is as follows:

 // <summary>        /// whether the system is        64-bit /// </summary>        /// <returns></returns>        Private BOOL is64bit ()        {            return8;        }

C # Operations Registry

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.