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 table /// summary> public class register {/// /// instance constructor /// < / summary> public register () {// // Todo: Add constructor logic //} #Region Public Method /// /// Write registry, if the specified item already exists, then modify The value of the specified item /// summary> /// Registry Basic enumeration param> /// Registration item, excluding base Item param> /// Value name param> /// Values param> /// Return to Boolean value, specify operation success returns> public bool setValue (XjxComponent.Register.keyType keytype, string key, string name, string values) {try {RegistryKey rk = (RegistryKey) getRegistryKey (keytype); RegistryKey rkt = rk.CreateSubKey (key); IF (rkt! = null) rkt.setValue (Name, VALUES); Else {throw ("" The item to be written does not exist "));} Return true;} catch {return false;}} /// /// Read Registry /// summary> /// Registry Classic enumeration param> /// registration Entries, excluding basic param> /// Value name param> /// Return string returns> public string getValue (xjxcomponent.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 the specified item ");} catCH (Exception EX) {Return EXMESSAGE;}} /// <
Summary> /// Delete the value in the registry /// summary> /// Registry Basic enumeration param> /// The registry key name does not include the base param> /// value name param> /// Returns the Boolean value, specifies whether the operation is successful returns> public bool deleteValue (XjxComponent.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 ("Unable to find specified item"); return true;} catch {return false;}} /// /// Delete the specified item in the registry / // summary> /// Registry Basic enumeration param> /// The item in the registry, does not include the foundation param> /// returns a Boolean value that specifies whether the operation was successful returns> public bool deleteSubKey (XjxComponent.Register.keyType keytype, string key) {try {RegistryKey rk = (RegistryKey) getRegistryKey (keytype); rk. DELETESUBKEYTREE (KEY); RETURN TRUE;} catch {return false;}} /// /// judgment The specified item exists /// summary> /// Classic enumeration param> /// Specify key string param> / // returns a Boolean value, indicating whether the specified item exists returns> public bool isExist (XjxComponent.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 (xjxcomponent.register. keyType keytype, string key) {RegistryKey rk = (RegistryKey) getRegistryKey (keytype); RegistryKey rkt = rk.OpenSubKey (key);! if (rkt = null) {string [] names = rkt.GetValueNames (); if (names .Length == 0) Return name; else {string [] value = new string [names.length]; int i = 0; Foreach (String name in name) {values [i] = rkt.getValue (name) .tostring ();}}} Else {throw ("Specify item does not exist"));}} #endregion #region private method /// /// Return RegistryKey object /// summary> /// Registry Basic enumeration param> /// returns> private object getRegistryKey (xjxcomponent.register.keytype keytype) {registryKey rk = NULL; SWITCH (Keytype) {copy keytype.hKey_class_root: rk = registry.classessroot; break; case keytyp e.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;} #ENDREGON #REGON enumeration /// /// Registry Basic Enumeration /// summary> public enum keytype: int {/// /// registration Table Classic HKEY_CLASSES_ROOT / / / summary> HKEY_CLASS_ROOT = 0, /// /// Registry Classic HKEY_CURRENT_USER / / <
/ summary> HKEY_CURRENT_USER = 1, /// /// Registry Class Item HKEY_LOCAL_MACHINE /// summary> HKEY_LOCAL_MACHINE = 2, /// /// Registry Classic HKEY_USERS / / / < / summary> hkey_users = 3, /// /// Registry Classic HKEY_CURRENT_CONFIG / / / Summary> HKEY_CURRENT_CONFIG = 4} #ENDREGON}}
http://blog.9cbs.net/czhenq/