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 /// summary> public class register {
/// /// instance constructor /// summary> 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 /// summary> /// Registry Basic enumeration param> /// Registration item, does not include the foundation param> /// Value name param> /// Value param> /// Returns the Boolean value, specifying whether the operation is successful returns> 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 /// summary> /// Registry Classic enumeration param> /// Registration item, excluding the foundation param> /// value name param> /// Returns 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 /// summary> /// Registry Basic enumeration param> /// Registration form name, does not include the foundation param> /// value name param> /// Returns the Boolean value, specifies whether the operation is successful returns> 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 param> /// The item in the registry does not include the reference param> /// to return to the Boolean value, specify whether the operation is successful returns> 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 /// summary> /// Classic enumeration param> /// Specify item string param> /// Returns the Boolean value, indicating whether the specified item exists returns> 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 /// summary> /// Classic enumeration param> /// Specify item string param> /// Returns the string array of all values associated with the specified item Returns> 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 /// summary> /// Registry Classic enumeration param> /// returns> 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 /// summary> public enum keytype: int {/// /// Registry Classic HKEY_CLASSES_ROOT / / / < / summary> hkey_class_root, /// /// Registry Circular HKEY_CURRENT_USER / / / Summary> HKEY_CURRENT_USER, /// /// Registry Courses HKEY_LOCAL_MACHINE /// summary> HKEY_LOCAL_MACHINE , /// /// Registry Classic hkey_users /// summary> HKEY_USERS, /// /// Registry Current_Current_config /// summary> hkey_current_config} #ENDREGON} }