Operate the class of the registry

xiaoxiao2021-03-06  62

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 /// 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 /// /// Registry Basic enumeration /// Registration item, excluding base Item /// Value name /// Values ​​ /// Return to Boolean value, specify operation success 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 /// /// Registry Classic enumeration /// registration Entries, excluding basic /// Value name /// Return string 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 /// /// Registry Basic enumeration /// The registry key name does not include the base /// value name /// Returns the Boolean value, specifies whether the operation is successful 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 / // /// Registry Basic enumeration /// The item in the registry, does not include the foundation /// returns a Boolean value that specifies whether the operation was successful 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 /// /// Classic enumeration /// Specify key string / // returns a Boolean value, indicating whether the specified item exists 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 /// /// Classic Enumeration

/// Specify item string /// Returns the string array of all values ​​associated with the specified item 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 /// /// Registry Basic enumeration /// 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 /// public enum keytype: int {/// /// registration Table Classic HKEY_CLASSES_ROOT / / / HKEY_CLASS_ROOT = 0, /// /// Registry Classic HKEY_CURRENT_USER / / <

/ summary> HKEY_CURRENT_USER = 1, ///

/// Registry Class Item HKEY_LOCAL_MACHINE /// HKEY_LOCAL_MACHINE = 2, /// /// Registry Classic HKEY_USERS / / / < / summary> hkey_users = 3, /// /// Registry Classic HKEY_CURRENT_CONFIG / / / HKEY_CURRENT_CONFIG = 4} #ENDREGON}}

http://blog.9cbs.net/czhenq/

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

New Post(0)