A class for an operational registry

xiaoxiao2021-03-06  72

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 the operation successfully public bool setValue (xjxcomponent.register.keytype keytype, string key, string name, string value) {TRY {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 /// Registry item, does not include the foundation /// Value name /// 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 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 (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 (New Exception "));

Return True;

} CatCh {returnaf false;}}

///

/// Remove the specified item in the registry ///////////////////////////////////////////////////////////> Enumeration /// Registry, does not include the basic /// Return to Boolean value, specify whether the operation is successful public bool deleteSubkey (xjxcomponent.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 ISEXIST (xjxcomponent.register.keytype keytype, string key) {registryKey rk = (registryKey) getRegistryKey KeyType;

IF (rk.opensubKey (key) == null) Return False; else return

///

/// Retrieves all values ​​associated with the specified item /// /// Classic enumeration /// Specified item string /// Returns the strings of all values ​​of the specified items public string [] getValues ​​(xjxcomponent.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 (XjxComponent.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 = 0, /// /// Registry Classic hkey_current_user /// hkey_current_user = 1, /// /// Registry Code HKEY_LOCAL_MACHINE / / / < / summary> HKEY_LOCAL_MACHINE = 2, /// /// Registry Classic hkey_users /// hkey_users = 3, /// /// Registry Classic HKEY_CURRENT_CONFIG /// < / summary> hkey_current_config = 4} #ENDREGON}}}

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

New Post(0)