A class for an operational registry

xiaoxiao2021-03-06  21

A class for an operational registry

Write a simple operational registry class in the morning. Now, let everyone help me see, please contact me if you have any questions. Using system; using microsoft.win32;

Namespace PowerComponent {///

/// Operation Registration /// public class register {

///

/// instance constructor /// public register () {// // Todo: Add constructor logic //}

#Region Public Method

///

/// Write the registry, if the specified item already exists, modify the value of the specified item /// /// Registry Basic enumeration /// Registration item, does not include the foundation /// Value name /// Value /// Returns the Boolean value, specifying whether the operation is successful public bool setValue (PowerComponent.register.Keytype Keytype, String Key, String Name, String Values) {TRING {RegistryKey rk = (registryKey) getRegistryKey (KeyType);

RegistryKey rkt = rk.createSubKey (key); if (rkt! = Null) rkt.setValue (name, value); else {throw (New Exception ");}

Return True;} catch {return false;}

}

///

/// read registry /// /// Registry Classic enumeration /// Registration item, excluding the foundation /// value name /// Returns Public String getValue (PowerComponent). Register.keytype keytype, string key, string name) {Try {registryKey rk = (registryKey) getRegistryKey (KeyType);

RegistryKey RKT = rk.opensubKey (key);

IF (rkt! = null) Return Rkt.getValue (Name) .tostring (); Else Throw ("Unable to find specified item");} catch (exception ex) {return ex. measure;}}

///

/// Remove the value in the registry /// /// Registry Basic enumeration /// Registration form name, does not include the foundation /// value name /// Returns the Boolean value, specifies whether the operation is successful public bool deleteValue (PowerComponent.Register.keyType keytype, string key, string name) {try {RegistryKey rk = (RegistryKey) getRegistryKey (keytype); RegistryKey rkt = rk.OpenSubKey (key, true);

IF (rkt! = null) rkt.deletevalue (name, true); Else Throw (New Exception ");

Return True;

} CatCh {returnaf false;}}

///

/// Remove the specified item in the registry ///////////////////////////////////////////////////////////> Enumeration /// The item in the registry does not include the reference /// to return to the Boolean value, specify whether the operation is successful public bool deleteSubkey (PowerComponent.Register.Keytype Keytype, String Key ) {Try {registryKey rk = (registryKey) getRegistryKey (keytype);

RK.DELETESUBKEYTREE (Key);

Return True;

} CatCh {returnaf false;}}

///

/// Judgment does the specified item exists /// /// Classic enumeration /// Specify item string /// Returns the Boolean value, indicating whether the specified item exists public bool zexist (powerbookponent.register.keytype keytype, string key) {registryKey rk = (registryKey) getRegistryKey KeyType;

IF (rk.opensubkey (key) == null) Return False; Else Return true;} ///

/// Retrieves all values ​​associated with the specified item /// /// Classic enumeration /// Specify item string /// Returns the string array of all values ​​associated with the specified item public string [] getValues ​​(PowerComponent.Register.Keytype Keytype, String Key) {registryKey rk = (registryKey) getRegistryKey (keytype);

RegistryKey RKT = rk.opensubKey (key);

IF (rkt! = null) {string [] name = rkt.getValuenames ();

IF (Names.Length == 0) Return Names; Else {String [] Values ​​= new string [names.length];

INT i = 0;

FOREACH (String Name in Names) {Values ​​[i] = rkt.getValue (name) .tostring ();

I;

Return Values;

}} Else {throw (new exception "));}}

#ndregion

#Region Private method

///

/// Returns RegistryKey object /// /// Registry Classic enumeration /// private object getRegistryKey (PowerComponent.Register.keyType keytype) {RegistryKey rk = null; switch (keytype) {case keyType.HKEY_CLASS_ROOT: rk = Registry.ClassesRoot; break; case keyType.HKEY_CURRENT_USER: rk = Registry.CurrentUser; break; case keyType .HKEY_LOCAL_MACHINE: rk = Registry.LocalMachine; break; case keyType.HKEY_USERS: rk = Registry.Users; break; case keyType.HKEY_CURRENT_CONFIG: rk = Registry.CurrentConfig; break;}

Return rk;}

#ndregion

#Region enumeration ///

/// Registry Classic enumeration /// public enum keytype: int {/// /// Registry Classic HKEY_CLASSES_ROOT / / / < / summary> hkey_class_root, /// /// Registry Circular HKEY_CURRENT_USER / / / HKEY_CURRENT_USER, /// /// Registry Courses HKEY_LOCAL_MACHINE /// HKEY_LOCAL_MACHINE , /// /// Registry Classic hkey_users /// HKEY_USERS, /// /// Registry Current_Current_config /// hkey_current_config} #ENDREGON} }

转载请注明原文地址:https://www.9cbs.com/read-44395.html

New Post(0)