A class for an operational registry

xiaoxiao2021-03-06  74

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 Registry Public Class Register {/// Instance Construction Function PUBLIC Register () {// Todo: Add Construction Function Logic here

#Region Public Method /// Write Registry, if the specified item already exists, modify the value of the specified item /// Registry Basic enumeration /// Registration item, excluding the foundation /// Value name /// Value /// returns a Boolean value, whether or not the operation succeeds 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 (New Exception ");} Return True;} catCh {returnaf false;}}

/// Read Registry /// Registry Basic enumeration /// Registration item, excluding basic /// Value name /// Return strings public string getValue (xjxcomponent.register.keytype keytype, string key, string name) {TRING {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;}

}

/// Delete the value in the registry /// Registry Basic enumeration /// Registry Name, excluding /// 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 ("If you cannot find a 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 item yl /// returns a Boolean value, whether or not the operation succeeds public bool deleteSubKey (XjxComponent.Register.keyType keytype, string key) {try {RegistryKey rk = (RegistryKey) getRegistryKey (keytype RK.DELETESUBKEYTREE (KEY); RETURN TRUE;} catCh {returnaf false;}}

/// Determine if the specified item exists /// Classic enumeration /// Specify key string /// < Returns> Returns the Boolean value to indicate 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

/// Retrieve all values ​​associated with the specified item /// Classic enumeration /// Specify item string // / returns a string associated with the specified item array of all values ​​ 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 Names; else {string [] values ​​= new string [names. Length]; int i = 0; forward (String name in name) {VALUES [I] = rkt.getValue (name) .tostring (); i ;} returnis;}} else {throw (new exception " does not exist")); } }

#ndregion

#Region Private method

/// Return 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 . = = Registration.Users; Break; Case keytype.hKey_current_config: rk = registry.currentconfig; Break;} Return RK;}

#ndregion

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

New Post(0)