In the morning, I wrote a simple class to operate the registry.
Please post the post for me to see if you have any questions.
Using system;
Using Microsoft. Win32;
Namespace powercomponent
{
/// <Summary>
/// Operate the Registry
/// </Summary>
Public class register
{
/// <Summary>
/// Instance Constructor
/// </Summary>
Public Register ()
{
//
// Todo: add the constructor logic here
//
}
# Region Public Method
/// <Summary>
/// Write to the Registry. If the specified item already exists, modify the value of the specified item.
/// </Summary>
/// <Param name = "keytype"> registry key enumeration </param>
/// <Param name = "key"> registry key, excluding the base key </param>
/// <Param name = "name"> value name </param>
/// <Param name = "values"> value </param>
/// <Returns> return a Boolean value, specifying whether the operation is successful </returns>
Public bool setvalue (powercomponent. Register. 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 ("the item to be written does not exist "));
}
Return true;
}
Catch
{
Return false;
}
}
/// <Summary>
/// Read the Registry
/// </Summary>
/// <Param name = "keytype"> registry key enumeration </param>
/// <Param name = "key"> registry key, excluding the base key </param>
/// <Param name = "name"> value name </param>
/// <Returns> return string </returns>
Public String getvalue (powercomponent. Register. 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 (new exception ("the specified item cannot be found "));
}
Catch (exception ex)
{
Return ex. message;
}
}
/// <Summary>
/// Delete the value in the Registry
/// </Summary>
/// <Param name = "keytype"> registry key enumeration </param>
/// <Param name = "key"> registry key name, excluding the base key </param>
/// <Param name = "name"> value name </param>
/// <Returns> return a Boolean value, specifying whether the operation is successful </returns>
Public bool deletevalue (powercomponent. Register. 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 ("the specified item cannot be found "));
Return true;
}
Catch
{
Return false;
}
}
/// <Summary>
/// Delete the specified entry in the Registry
/// </Summary>
/// <Param name = "keytype"> registry key enumeration </param>
/// <Param name = "key"> key in the registry, excluding the base item </param>
/// <Returns> return a Boolean value, specifying whether the operation is successful </returns>
Public bool deletesubkey (powercomponent. Register. keytype, string key)
{
Try
{
Registrykey rk = (registrykey) getregistrykey (keytype );
Rk. deletesubkeytree (key );
Return true;
}
Catch
{
Return false;
}
}
/// <Summary>
/// Determine whether a specified item exists
/// </Summary>
/// <Param name = "keytype"> base item enumeration </param>
/// <Param name = "key"> specified string </param>
/// <Returns> Returns a Boolean value indicating whether the specified item exists. </returns>
Public bool isexist (powercomponent. Register. keytype, string key)
{
Registrykey rk = (registrykey) getregistrykey (keytype );
If (rk. opensubkey (key) = NULL)
Return false;
Else
Return true;
}
/// <Summary>
/// Retrieve all values associated with a specified item
/// </Summary>
/// <Param name = "keytype"> base item enumeration </param>
/// <Param name = "key"> specified string </param>
/// <Returns> returns the string array of all values associated with the specified item </returns>
Public String [] getvalues (powercomponent. Register. 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;
Foreach (string name in names)
{
Values [I] = RKT. getvalue (name). tostring ();
I ++;
}
Return values;
}
}
Else
{
Throw (new exception ("the specified item does not exist "));
}
}
# Endregion
# Region private Method
/// <Summary>
/// Return the registrykey object
/// </Summary>
/// <Param name = "keytype"> registry key enumeration </param>
/// <Returns> </returns>
Private object getregistrykey (powercomponent. Register. 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;
}
# Endregion
# Region Enumeration
/// <Summary>
/// Registry key Enumeration
/// </Summary>
Public Enum keytype: int
{
/// <Summary>
/// Registry key hkey_classes_root
/// </Summary>
Hkey_class_root,
/// <Summary>
/// Registry key HKEY_CURRENT_USER
/// </Summary>
HKEY_CURRENT_USER,
/// <Summary>
/// Registry key HKEY_LOCAL_MACHINE
/// </Summary>
HKEY_LOCAL_MACHINE,
/// <Summary>
/// Registry key HKEY_USERS
/// </Summary>
HKEY_USERS,
/// <Summary>
/// Registry key hkey_current_config
/// </Summary>
Hkey_current_config
}
# Endregion
}
}