Re-encapsulation of the registry read-write class

Source: Internet
Author: User
Tags bool
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;
}
}
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.