Re-encapsulation of the registry read-write class
Last Update:2017-02-28
Source: Internet
Author: User
Encapsulation | Registry using System;
Using System.IO;
Using System.Runtime.InteropServices;
Using System.Text;
Using Microsoft.Win32;
Namespace Wjb.readorwriteiniandreg
{
<summary>
Summary description of the Rwreg.
Registry Action Class
Class Library Development: Wu Jianbing
Time: November 07, 2003
Features: Registry operations
</summary>
public class Rwreg
{
private static RegistryKey Rootkey;
<summary>
The Registry action class that constructs the root key is Rootkey, and the Current_User primary key is opened by default
</summary>
Public Rwreg (String rootkey)
{
Switch (Rootkey.toupper ())
{
Case "Classes_root":
Rootkey=registry.classesroot;
Break
Case "Current_User":
Rootkey=registry.currentuser;
Break
Case "Local_machine":
Rootkey=registry.localmachine;
Break
Case "USERS":
Rootkey=registry.users;
Break
Case "Current_config":
Rootkey=registry.currentconfig;
Break
Case "Dyn_data":
Rootkey=registry.dyndata;
Break
Case "Performance_data":
Rootkey=registry.performancedata;
Break
Default
Rootkey=registry.currentuser;
Break
}
}
<summary>
Read path is KeyPath, key is KeyName registry key value, the default return def
</summary>
<param name= "KeyPath" ></param>
<param name= "KeyName" ></param>
<param name= "def" ></param>
<returns></returns>
public string Getregval (string keypath,string keyname,string def)
{
Try
{
RegistryKey Key=rootkey. OpenSubKey (KeyPath);
Return key. GetValue (KeyName, (object) def). ToString ();
}
Catch
{
return def;
}
}
<summary>
The setting path is KeyPath, and the registry key named KeyName is Keyval
</summary>
<param name= "KeyPath" ></param>
<param name= "KeyName" ></param>
<param name= "Keyval" ></param>
public bool Setregval (string keypath,string keyname,string keyval)
{
Try
{
RegistryKey Key=rootkey. OpenSubKey (keypath,true);
Key. SetValue (KeyName, (object) keyval);
return true;
}
Catch
{
return false;
}
}
<summary>
Create a key with a path of keypath
</summary>
<param name= "KeyPath" ></param>
<returns></returns>
Public RegistryKey Createregkey (string keypath)
{
Try
{
Return Rootkey. CreateSubKey (KeyPath);
}
Catch
{
return null;
}
}
<summary>
Delete a subkey with a path of keypath
</summary>
<param name= "KeyPath" ></param>
<returns></returns>
public bool Delregsubkey (string keypath)
{
Try
{
Rootkey. DeleteSubKey (KeyPath);
return true;
}
Catch
{
return false;
}
}
<summary>
Delete a subkey with a path of KeyPath and its subordinate children
</summary>
<param name= "KeyPath" ></param>
<returns></returns>
public bool Delregsubkeytree (string keypath)
{
Try
{
Rootkey. Deletesubkeytree (KeyPath);
return true;
}
Catch
{
return false;
}
}
<summary>
Delete Path is the key value of the KeyPath key named KeyName
</summary>
<param name= "KeyPath" ></param>
<param name= "KeyName" ></param>
<returns></returns>
public bool Delregkeyval (string keypath,string keyname)
{
Try
{
RegistryKey Key=rootkey. OpenSubKey (keypath,true);
Key. DeleteValue (KeyName);
return true;
}
Catch
{
return false;
}
}
}
}