對註冊表讀寫類的再封裝

來源:互聯網
上載者:User
封裝|註冊表 using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32;

namespace Wjb.ReadOrWriteIniAndReg
{
/// <summary>
/// RWReg 的摘要說明。
/// 註冊表操作類
/// 類庫開發:吳劍冰
/// 時間:2003年11月07日
/// 功能:註冊表操作
/// </summary>
public class RWReg
{
private static RegistryKey rootkey;
/// <summary>
/// 構造根鍵為RootKey的註冊表操作類,預設開啟Current_User主鍵
/// </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>
/// 讀取路徑為keypath,鍵名為keyname的註冊表索引值,預設返回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>
/// 設定路徑為keypath,鍵名為keyname的註冊表索引值為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>
/// 建立路徑為keypath的鍵
/// </summary>
/// <param name="keypath"></param>
/// <returns></returns>
public RegistryKey CreateRegKey(string keypath)
{
try
{
return rootkey.CreateSubKey(keypath);
}
catch
{
return null;
}
}
/// <summary>
/// 刪除路徑為keypath的子項
/// </summary>
/// <param name="keypath"></param>
/// <returns></returns>
public bool DelRegSubKey(string keypath)
{
try
{
rootkey.DeleteSubKey(keypath);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 刪除路徑為keypath的子項及其附屬子項
/// </summary>
/// <param name="keypath"></param>
/// <returns></returns>
public bool DelRegSubKeyTree(string keypath)
{
try
{
rootkey.DeleteSubKeyTree(keypath);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 刪除路徑為keypath下鍵名為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;
}
}
}
}


相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。