程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#注冊表操作類分享

c#注冊表操作類分享

編輯:C#入門知識

c#注冊表操作類分享。本站提示廣大學習愛好者:(c#注冊表操作類分享)文章只能為提供參考,不一定能成為您想要的結果。以下是c#注冊表操作類分享正文



/// <summary>
/// 注冊表基項靜態域
///
/// 重要包含:
/// 1.Registry.ClassesRoot 對應於HKEY_CLASSES_ROOT主鍵
/// 2.Registry.CurrentUser 對應於HKEY_CURRENT_USER主鍵
/// 3.Registry.LocalMachine 對應於 HKEY_LOCAL_MACHINE主鍵
/// 4.Registry.User 對應於 HKEY_USER主鍵
/// 5.Registry.CurrentConfig 對應於HEKY_CURRENT_CONFIG主鍵
/// 6.Registry.DynDa 對應於HKEY_DYN_DATA主鍵
/// 7.Registry.PerformanceData 對應於HKEY_PERFORMANCE_DATA主鍵
///
/// 版本:1.0
/// </summary>
public enum RegDomain
{
/// <summary>
/// 對應於HKEY_CLASSES_ROOT主鍵
/// </summary>
ClassesRoot = 0,
/// <summary>
/// 對應於HKEY_CURRENT_USER主鍵
/// </summary>
CurrentUser = 1,
/// <summary>
/// 對應於 HKEY_LOCAL_MACHINE主鍵
/// </summary>
LocalMachine = 2,
/// <summary>
/// 對應於 HKEY_USER主鍵
/// </summary>
User = 3,
/// <summary>
/// 對應於HEKY_CURRENT_CONFIG主鍵
/// </summary>
CurrentConfig = 4,
/// <summary>
/// 對應於HKEY_DYN_DATA主鍵
/// </summary>
DynDa = 5,
/// <summary>
/// 對應於HKEY_PERFORMANCE_DATA主鍵
/// </summary>
PerformanceData = 6,
}

/// <summary>
/// 指定在注冊表中存儲值時所用的數據類型,或標識注冊表中某個值的數據類型
///
/// 重要包含:
/// 1.RegistryValueKind.Unknown
/// 2.RegistryValueKind.String
/// 3.RegistryValueKind.ExpandString
/// 4.RegistryValueKind.Binary
/// 5.RegistryValueKind.DWord
/// 6.RegistryValueKind.MultiString
/// 7.RegistryValueKind.QWord
///
/// 版本:1.0
/// </summary>
public enum RegValueKind
{
/// <summary>
/// 指導一個不受支撐的注冊表數據類型。例如,不支撐 Microsoft Win32 API 注冊表數據類型 REG_RESOURCE_LIST。應用此值指定
/// </summary>
Unknown = 0,
/// <summary>
/// 指定一個以 Null 開頭的字符串。此值與 Win32 API 注冊表數據類型 REG_SZ 等效。
/// </summary>
String = 1,
/// <summary>
/// 指定一個以 NULL 開頭的字符串,該字符串中包括對情況變量(如 %PATH%,當值被檢索時,就會睜開)的未睜開的援用。
/// 此值與 Win32 API注冊表數據類型 REG_EXPAND_SZ 等效。
/// </summary>
ExpandString = 2,
/// <summary>
/// 指定隨意率性格局的二進制數據。此值與 Win32 API 注冊表數據類型 REG_BINARY 等效。
/// </summary>
Binary = 3,
/// <summary>
/// 指定一個 32 位二進制數。此值與 Win32 API 注冊表數據類型 REG_DWORD 等效。
/// </summary>
DWord = 4,
/// <summary>
/// 指定一個以 NULL 開頭的字符串數組,以兩個空字符停止。此值與 Win32 API 注冊表數據類型 REG_MULTI_SZ 等效。
/// </summary>
MultiString = 5,
/// <summary>
/// 指定一個 64 位二進制數。此值與 Win32 API 注冊表數據類型 REG_QWORD 等效。
/// </summary>
QWord = 6,
}

/// <summary>
/// 注冊表操作類
///
/// 重要包含以下操作:
/// 1.創立注冊表項
/// 2.讀取注冊表項
/// 3.斷定注冊表項能否存在
/// 4.刪除注冊表項
/// 5.創立注冊表鍵值
/// 6.讀取注冊表鍵值
/// 7.斷定注冊表鍵值能否存在
/// 8.刪除注冊表鍵值
///
/// 版本:1.0
/// </summary>
public class Register
{
#region 字段界說
/// <summary>
/// 注冊表項稱號
/// </summary>
private string _subkey;
/// <summary>
/// 注冊表基項域
/// </summary>
private RegDomain _domain;
/// <summary>
/// 注冊表鍵值
/// </summary>
private string _regeditkey;
#endregion

#region 屬性
/// <summary>
/// 設置注冊表項稱號
/// </summary>
public string SubKey
{
//get { return _subkey; }
set { _subkey = value; }
}

/// <summary>
/// 注冊表基項域
/// </summary>
public RegDomain Domain
{
///get { return _domain; }
set { _domain = value; }
}

/// <summary>
/// 注冊表鍵值
/// </summary>
public string RegeditKey
{
///get{return _regeditkey;}
set { _regeditkey = value; }
}
#endregion

#region 結構函數
public Register()
{
///默許注冊表項稱號
_subkey = "software\\";
///默許注冊表基項域
_domain = RegDomain.LocalMachine;
}

/// <summary>
/// 結構函數
/// </summary>
/// <param name="subKey">注冊表項稱號</param>
/// <param name="regDomain">注冊表基項域</param>
public Register(string subKey, RegDomain regDomain)
{
///設置注冊表項稱號
_subkey = subKey;
///設置注冊表基項域
_domain = regDomain;
}
#endregion

#region 私有辦法
#region 創立注冊表項
/// <summary>
/// 創立注冊表項,默許創立在注冊表基項 HKEY_LOCAL_MACHINE上面(請先設置SubKey屬性)
/// 虛辦法,子類可停止重寫
/// </summary>
public virtual void CreateSubKey()
{
///斷定注冊表項稱號能否為空,假如為空,前往false
if (_subkey == string.Empty || _subkey == null)
{
return;
}

///創立基於注冊表基項的節點
RegistryKey key = GetRegDomain(_domain);

///要創立的注冊表項的節點
RegistryKey sKey;
if (!IsSubKeyExist())
{
sKey = key.CreateSubKey(_subkey);
}
//sKey.Close();
///封閉對注冊表項的更改
key.Close();
}

/// <summary>
/// 創立注冊表項,默許創立在注冊表基項 HKEY_LOCAL_MACHINE上面
/// 虛辦法,子類可停止重寫
/// 例子:如subkey是software\\higame\\,則將創立HKEY_LOCAL_MACHINE\\software\\higame\\注冊表項
/// </summary>
/// <param name="subKey">注冊表項稱號</param>
public virtual void CreateSubKey(string subKey)
{
///斷定注冊表項稱號能否為空,假如為空,前往false
if (subKey == string.Empty || subKey == null)
{
return;
}

///創立基於注冊表基項的節點
RegistryKey key = GetRegDomain(_domain);

///要創立的注冊表項的節點
RegistryKey sKey;
if (!IsSubKeyExist(subKey))
{
sKey = key.CreateSubKey(subKey);
}
//sKey.Close();
///封閉對注冊表項的更改
key.Close();
}

/// <summary>
/// 創立注冊表項,默許創立在注冊表基項 HKEY_LOCAL_MACHINE上面
/// 虛辦法,子類可停止重寫
/// </summary>
/// <param name="regDomain">注冊表基項域</param>
public virtual void CreateSubKey(RegDomain regDomain)
{
///斷定注冊表項稱號能否為空,假如為空,前往false
if (_subkey == string.Empty || _subkey == null)
{
return;
}

///創立基於注冊表基項的節點
RegistryKey key = GetRegDomain(regDomain);

///要創立的注冊表項的節點
RegistryKey sKey;
if (!IsSubKeyExist(regDomain))
{
sKey = key.CreateSubKey(_subkey);
}
//sKey.Close();
///封閉對注冊表項的更改
key.Close();
}

/// <summary>
/// 創立注冊表項(請先設置SubKey屬性)
/// 虛辦法,子類可停止重寫
/// 例子:如regDomain是HKEY_LOCAL_MACHINE,subkey是software\\higame\\,則將創立HKEY_LOCAL_MACHINE\\software\\higame\\注冊表項
/// </summary>
/// <param name="subKey">注冊表項稱號</param>
/// <param name="regDomain">注冊表基項域</param>
public virtual void CreateSubKey(string subKey, RegDomain regDomain)
{
///斷定注冊表項稱號能否為空,假如為空,前往false
if (subKey == string.Empty || subKey == null)
{
return;
}

///創立基於注冊表基項的節點
RegistryKey key = GetRegDomain(regDomain);

///要創立的注冊表項的節點
RegistryKey sKey;
if (!IsSubKeyExist(subKey, regDomain))
{
sKey = key.CreateSubKey(subKey);
}
//sKey.Close();
///封閉對注冊表項的更改
key.Close();
}
#endregion

#region 斷定注冊表項能否存在
/// <summary>
/// 斷定注冊表項能否存在,默許是在注冊表基項HKEY_LOCAL_MACHINE下斷定(請先設置SubKey屬性)
/// 虛辦法,子類可停止重寫
/// 例子:假如設置了Domain和SubKey屬性,則斷定Domain\\SubKey,不然默許斷定HKEY_LOCAL_MACHINE\\software\\
/// </summary>
/// <returns>前往注冊表項能否存在,存在前往true,不然前往false</returns>
public virtual bool IsSubKeyExist()
{
///斷定注冊表項稱號能否為空,假如為空,前往false
if (_subkey == string.Empty || _subkey == null)
{
return false;
}
///檢索注冊表子項
///假如sKey為null,解釋沒有該注冊表項不存在,不然存在
RegistryKey sKey = OpenSubKey(_subkey, _domain);
if (sKey == null)
{
return false;
}
return true;
}
/// <summary>
/// 斷定注冊表項能否存在,默許是在注冊表基項HKEY_LOCAL_MACHINE下斷定
/// 虛辦法,子類可停止重寫
/// 例子:如subkey是software\\higame\\,則將斷定HKEY_LOCAL_MACHINE\\software\\higame\\注冊表項能否存在
/// </summary>
/// <param name="subKey">注冊表項稱號</param>
/// <returns>前往注冊表項能否存在,存在前往true,不然前往false</returns>
public virtual bool IsSubKeyExist(string subKey)
{
///斷定注冊表項稱號能否為空,假如為空,前往false
if (subKey == string.Empty || subKey == null)
{
return false;
}
///檢索注冊表子項
///假如sKey為null,解釋沒有該注冊表項不存在,不然存在
RegistryKey sKey = OpenSubKey(subKey);
if (sKey == null)
{
return false;
}
return true;
}
/// <summary>
/// 斷定注冊表項能否存在
/// 虛辦法,子類可停止重寫
/// 例子:如regDomain是HKEY_CLASSES_ROOT,則將斷定HKEY_CLASSES_ROOT\\SubKey注冊表項能否存在
/// </summary>
/// <param name="regDomain">注冊表基項域</param>
/// <returns>前往注冊表項能否存在,存在前往true,不然前往false</returns>
public virtual bool IsSubKeyExist(RegDomain regDomain)
{
///斷定注冊表項稱號能否為空,假如為空,前往false
if (_subkey == string.Empty || _subkey == null)
{
return false;
}
///檢索注冊表子項
///假如sKey為null,解釋沒有該注冊表項不存在,不然存在
RegistryKey sKey = OpenSubKey(_subkey, regDomain);
if (sKey == null)
{
return false;
}
return true;
}
/// <summary>
/// 斷定注冊表項能否存在(請先設置SubKey屬性)
/// 虛辦法,子類可停止重寫
/// 例子:如regDomain是HKEY_CLASSES_ROOT,subkey是software\\higame\\,則將斷定HKEY_CLASSES_ROOT\\software\\higame\\注冊表項能否存在
/// </summary>
/// <param name="subKey">注冊表項稱號</param>
/// <param name="regDomain">注冊表基項域</param>
/// <returns>前往注冊表項能否存在,存在前往true,不然前往false</returns>
public virtual bool IsSubKeyExist(string subKey, RegDomain regDomain)
{
///斷定注冊表項稱號能否為空,假如為空,前往false
if (subKey == string.Empty || subKey == null)
{
return false;
}
///檢索注冊表子項
///假如sKey為null,解釋沒有該注冊表項不存在,不然存在
RegistryKey sKey = OpenSubKey(subKey, regDomain);
if (sKey == null)
{
return false;
}
return true;
}
#endregion
#region 刪除注冊表項
/// <summary>
/// 刪除注冊表項(請先設置SubKey屬性)
/// 虛辦法,子類可停止重寫
/// </summary>
/// <returns>假如刪除勝利,則前往true,不然為false</returns>
public virtual bool DeleteSubKey()
{
///前往刪除能否勝利
bool result = false;
///斷定注冊表項稱號能否為空,假如為空,前往false
if (_subkey == string.Empty || _subkey == null)
{
return false;
}
///創立基於注冊表基項的節點
RegistryKey key = GetRegDomain(_domain);
if (IsSubKeyExist())
{
try
{
///刪除注冊表項
key.DeleteSubKey(_subkey);
result = true;
}
catch
{
result = false;
}
}
///封閉對注冊表項的更改
key.Close();
return result;
}
/// <summary>
/// 刪除注冊表項(請先設置SubKey屬性)
/// 虛辦法,子類可停止重寫
/// </summary>
/// <param name="subKey">注冊表項稱號</param>
/// <returns>假如刪除勝利,則前往true,不然為false</returns>
public virtual bool DeleteSubKey(string subKey)
{
///前往刪除能否勝利
bool result = false;
///斷定注冊表項稱號能否為空,假如為空,前往false
if (subKey == string.Empty || subKey == null)
{
return false;
}
///創立基於注冊表基項的節點
RegistryKey key = GetRegDomain(_domain);
if (IsSubKeyExist())
{
try
{
///刪除注冊表項
key.DeleteSubKey(subKey);
result = true;
}
catch
{
result = false;
}
}
///封閉對注冊表項的更改
key.Close();
return result;
}
/// <summary>
/// 刪除注冊表項
/// 虛辦法,子類可停止重寫
/// </summary>
/// <param name="subKey">注冊表項稱號</param>
/// <param name="regDomain">注冊表基項域</param>
/// <returns>假如刪除勝利,則前往true,不然為false</returns>
public virtual bool DeleteSubKey(string subKey, RegDomain regDomain)
{
///前往刪除能否勝利
bool result = false;
///斷定注冊表項稱號能否為空,假如為空,前往false
if (subKey == string.Empty || subKey == null)
{
return false;
}
///創立基於注冊表基項的節點
RegistryKey key = GetRegDomain(regDomain);
if (IsSubKeyExist(subKey, regDomain))
{
try
{
///刪除注冊表項
key.DeleteSubKey(subKey);
result = true;
}
catch
{
result = false;
}
}
///封閉對注冊表項的更改
key.Close();
return result;
}
#endregion
#region 斷定鍵值能否存在
/// <summary>
/// 斷定鍵值能否存在(請先設置SubKey和RegeditKey屬性)
/// 虛辦法,子類可停止重寫
/// 1.假如RegeditKey為空、null,則前往false
/// 2.假如SubKey為空、null或許SubKey指定的注冊表項不存在,前往false
/// </summary>
/// <returns>前往鍵值能否存在,存在前往true,不然前往false</returns>
public virtual bool IsRegeditKeyExist()
{
///前往成果
bool result = false;
///斷定能否設置鍵值屬性
if (_regeditkey == string.Empty || _regeditkey == null)
{
return false;
}
///斷定注冊表項能否存在
if (IsSubKeyExist())
{
///翻開注冊表項
RegistryKey key = OpenSubKey();
///鍵值聚集
string[] regeditKeyNames;
///獲得鍵值聚集
regeditKeyNames = key.GetValueNames();
///遍歷鍵值聚集,假如存在鍵值,則加入遍歷
foreach (string regeditKey in regeditKeyNames)
{
if (string.Compare(regeditKey, _regeditkey, true) == 0)
{
result = true;
break;
}
}
///封閉對注冊表項的更改
key.Close();
}
return result;
}
/// <summary>
/// 斷定鍵值能否存在(請先設置SubKey屬性)
/// 虛辦法,子類可停止重寫
/// 假如SubKey為空、null或許SubKey指定的注冊表項不存在,前往false
/// </summary>
/// <param name="name">鍵值稱號</param>
/// <returns>前往鍵值能否存在,存在前往true,不然前往false</returns>
public virtual bool IsRegeditKeyExist(string name)
{
///前往成果
bool result = false;
///斷定能否設置鍵值屬性
if (name == string.Empty || name == null)
{
return false;
}
///斷定注冊表項能否存在
if (IsSubKeyExist())
{
///翻開注冊表項
RegistryKey key = OpenSubKey();
///鍵值聚集
string[] regeditKeyNames;
///獲得鍵值聚集
regeditKeyNames = key.GetValueNames();
///遍歷鍵值聚集,假如存在鍵值,則加入遍歷
foreach (string regeditKey in regeditKeyNames)
{
if (string.Compare(regeditKey, name, true) == 0)
{
result = true;
break;
}
}
///封閉對注冊表項的更改
key.Close();
}
return result;
}
/// <summary>
/// 斷定鍵值能否存在
/// 虛辦法,子類可停止重寫
/// </summary>
/// <param name="name">鍵值稱號</param>
/// <param name="subKey">注冊表項稱號</param>
/// <returns>前往鍵值能否存在,存在前往true,不然前往false</returns>
public virtual bool IsRegeditKeyExist(string name, string subKey)
{
///前往成果
bool result = false;
///斷定能否設置鍵值屬性
if (name == string.Empty || name == null)
{
return false;
}
///斷定注冊表項能否存在
if (IsSubKeyExist())
{
///翻開注冊表項
RegistryKey key = OpenSubKey(subKey);
///鍵值聚集
string[] regeditKeyNames;
///獲得鍵值聚集
regeditKeyNames = key.GetValueNames();
///遍歷鍵值聚集,假如存在鍵值,則加入遍歷
foreach (string regeditKey in regeditKeyNames)
{
if (string.Compare(regeditKey, name, true) == 0)
{
result = true;
break;
}
}
///封閉對注冊表項的更改
key.Close();
}
return result;
}
/// <summary>
/// 斷定鍵值能否存在
/// 虛辦法,子類可停止重寫
/// </summary>
/// <param name="name">鍵值稱號</param>
/// <param name="subKey">注冊表項稱號</param>
/// <param name="regDomain">注冊表基項域</param>
/// <returns>前往鍵值能否存在,存在前往true,不然前往false</returns>
public virtual bool IsRegeditKeyExist(string name, string subKey, RegDomain regDomain)
{
///前往成果
bool result = false;
///斷定能否設置鍵值屬性
if (name == string.Empty || name == null)
{
return false;
}
///斷定注冊表項能否存在
if (IsSubKeyExist())
{
///翻開注冊表項
RegistryKey key = OpenSubKey(subKey, regDomain);
///鍵值聚集
string[] regeditKeyNames;
///獲得鍵值聚集
regeditKeyNames = key.GetValueNames();
///遍歷鍵值聚集,假如存在鍵值,則加入遍歷
foreach (string regeditKey in regeditKeyNames)
{
if (string.Compare(regeditKey, name, true) == 0)
{
result = true;
break;
}
}
///封閉對注冊表項的更改
key.Close();
}
return result;
}
#endregion
#region 設置鍵值內容
/// <summary>
/// 設置指定的鍵值內容,不指定內容數據類型(請先設置RegeditKey和SubKey屬性)
/// 存在改鍵值則修正鍵值內容,不存在鍵值則先創立鍵值,再設置鍵值內容
/// </summary>
/// <param name="content">鍵值內容</param>
/// <returns>鍵值內容設置勝利,則前往true,不然前往false</returns>
public virtual bool WriteRegeditKey(object content)
{
///前往成果
bool result = false;
///斷定能否設置鍵值屬性
if (_regeditkey == string.Empty || _regeditkey == null)
{
return false;
}
///斷定注冊表項能否存在,假如不存在,則直接創立
if (!IsSubKeyExist(_subkey))
{
CreateSubKey(_subkey);
}
///以可寫方法翻開注冊表項
RegistryKey key = OpenSubKey(true);
///假如注冊表項翻開掉敗,則前往false
if (key == null)
{
return false;
}
try
{
key.SetValue(_regeditkey, content);
result = true;
}
catch
{
result = false;
}
finally
{
///封閉對注冊表項的更改
key.Close();
}
return result;
}
/// <summary>
/// 設置指定的鍵值內容,不指定內容數據類型(請先設置SubKey屬性)
/// 存在改鍵值則修正鍵值內容,不存在鍵值則先創立鍵值,再設置鍵值內容
/// </summary>
/// <param name="name">鍵值稱號</param>
/// <param name="content">鍵值內容</param>
/// <returns>鍵值內容設置勝利,則前往true,不然前往false</returns>
public virtual bool WriteRegeditKey(string name, object content)
{
///前往成果
bool result = false;
///斷定鍵值能否存在
if (name == string.Empty || name == null)
{
return false;
}
///斷定注冊表項能否存在,假如不存在,則直接創立
if (!IsSubKeyExist(_subkey))
{
CreateSubKey(_subkey);
}
///以可寫方法翻開注冊表項
RegistryKey key = OpenSubKey(true);
///假如注冊表項翻開掉敗,則前往false
if (key == null)
{
return false;
}
try
{
key.SetValue(name, content);
result = true;
}
catch
{
result = false;
}
finally
{
///封閉對注冊表項的更改
key.Close();
}
return result;
}
/// <summary>
/// 設置指定的鍵值內容,指定內容數據類型(請先設置SubKey屬性)
/// 存在改鍵值則修正鍵值內容,不存在鍵值則先創立鍵值,再設置鍵值內容
/// </summary>
/// <param name="name">鍵值稱號</param>
/// <param name="content">鍵值內容</param>
/// <returns>鍵值內容設置勝利,則前往true,不然前往false</returns>
public virtual bool WriteRegeditKey(string name, object content, RegValueKind regValueKind)
{
///前往成果
bool result = false;
///斷定鍵值能否存在
if (name == string.Empty || name == null)
{
return false;
}
///斷定注冊表項能否存在,假如不存在,則直接創立
if (!IsSubKeyExist(_subkey))
{
CreateSubKey(_subkey);
}
///以可寫方法翻開注冊表項
RegistryKey key = OpenSubKey(true);
///假如注冊表項翻開掉敗,則前往false
if (key == null)
{
return false;
}
try
{
key.SetValue(name, content, GetRegValueKind(regValueKind));
result = true;
}
catch
{
result = false;
}
finally
{
///封閉對注冊表項的更改
key.Close();
}
return result;
}
#endregion
#region 讀取鍵值內容
/// <summary>
/// 讀取鍵值內容(請先設置RegeditKey和SubKey屬性)
/// 1.假如RegeditKey為空、null或許RegeditKey指導的鍵值不存在,前往null
/// 2.假如SubKey為空、null或許SubKey指導的注冊表項不存在,前往null
/// 3.反之,則前往鍵值內容
/// </summary>
/// <returns>前往鍵值內容</returns>
public virtual object ReadRegeditKey()
{
///鍵值內容成果
object obj = null;
///斷定能否設置鍵值屬性
if (_regeditkey == string.Empty || _regeditkey == null)
{
return null;
}
///斷定鍵值能否存在
if (IsRegeditKeyExist(_regeditkey))
{
///翻開注冊表項
RegistryKey key = OpenSubKey();
if (key != null)
{
obj = key.GetValue(_regeditkey);
}
///封閉對注冊表項的更改
key.Close();
}
return obj;
}
/// <summary>
/// 讀取鍵值內容(請先設置SubKey屬性)
/// 1.假如SubKey為空、null或許SubKey指導的注冊表項不存在,前往null
/// 2.反之,則前往鍵值內容
/// </summary>
/// <param name="name">鍵值稱號</param>
/// <returns>前往鍵值內容</returns>
public virtual object ReadRegeditKey(string name)
{
///鍵值內容成果
object obj = null;
///斷定能否設置鍵值屬性
if (name == string.Empty || name == null)
{
return null;
}
///斷定鍵值能否存在
if (IsRegeditKeyExist(name))
{
///翻開注冊表項
RegistryKey key = OpenSubKey();
if (key != null)
{
obj = key.GetValue(name);
}
///封閉對注冊表項的更改
key.Close();
}
return obj;
}
/// <summary>
/// 讀取鍵值內容
/// </summary>
/// <param name="name">鍵值稱號</param>
/// <param name="subKey">注冊表項稱號</param>
/// <returns>前往鍵值內容</returns>
public virtual object ReadRegeditKey(string name, string subKey)
{
///鍵值內容成果
object obj = null;
///斷定能否設置鍵值屬性
if (name == string.Empty || name == null)
{
return null;
}
///斷定鍵值能否存在
if (IsRegeditKeyExist(name))
{
///翻開注冊表項
RegistryKey key = OpenSubKey(subKey);
if (key != null)
{
obj = key.GetValue(name);
}
///封閉對注冊表項的更改
key.Close();
}
return obj;
}
/// <summary>
/// 讀取鍵值內容
/// </summary>
/// <param name="name">鍵值稱號</param>
/// <param name="subKey">注冊表項稱號</param>
/// <param name="regDomain">注冊表基項域</param>
/// <returns>前往鍵值內容</returns>
public virtual object ReadRegeditKey(string name, string subKey, RegDomain regDomain)
{
///鍵值內容成果
object obj = null;
///斷定能否設置鍵值屬性
if (name == string.Empty || name == null)
{
return null;
}
///斷定鍵值能否存在
if (IsRegeditKeyExist(name))
{
///翻開注冊表項
RegistryKey key = OpenSubKey(subKey, regDomain);
if (key != null)
{
obj = key.GetValue(name);
}
///封閉對注冊表項的更改
key.Close();
}
return obj;
}
#endregion
#region 刪除鍵值
/// <summary>
/// 刪除鍵值(請先設置RegeditKey和SubKey屬性)
/// 1.假如RegeditKey為空、null或許RegeditKey指導的鍵值不存在,前往false
/// 2.假如SubKey為空、null或許SubKey指導的注冊表項不存在,前往false
/// </summary>
/// <returns>假如刪除勝利,前往true,不然前往false</returns>
public virtual bool DeleteRegeditKey()
{
///刪除成果
bool result = false;
///斷定能否設置鍵值屬性,假如沒有設置,則前往false
if (_regeditkey == string.Empty || _regeditkey == null)
{
return false;
}
///斷定鍵值能否存在
if (IsRegeditKeyExist(_regeditkey))
{
///以可寫方法翻開注冊表項
RegistryKey key = OpenSubKey(true);
if (key != null)
{
try
{
///刪除鍵值
key.DeleteValue(_regeditkey);
result = true;
}
catch
{
result = false;
}
finally
{
///封閉對注冊表項的更改
key.Close();
}
}
}
return result;
}
/// <summary>
/// 刪除鍵值(請先設置SubKey屬性)
/// 假如SubKey為空、null或許SubKey指導的注冊表項不存在,前往false
/// </summary>
/// <param name="name">鍵值稱號</param>
/// <returns>假如刪除勝利,前往true,不然前往false</returns>
public virtual bool DeleteRegeditKey(string name)
{
///刪除成果
bool result = false;
///斷定鍵值稱號能否為空,假如為空,則前往false
if (name == string.Empty || name == null)
{
return false;
}
///斷定鍵值能否存在
if (IsRegeditKeyExist(name))
{
///以可寫方法翻開注冊表項
RegistryKey key = OpenSubKey(true);
if (key != null)
{
try
{
///刪除鍵值
key.DeleteValue(name);
result = true;
}
catch
{
result = false;
}
finally
{
///封閉對注冊表項的更改
key.Close();
}
}
}
return result;
}
/// <summary>
/// 刪除鍵值
/// </summary>
/// <param name="name">鍵值稱號</param>
/// <param name="subKey">注冊表項稱號</param>
/// <returns>假如刪除勝利,前往true,不然前往false</returns>
public virtual bool DeleteRegeditKey(string name, string subKey)
{
///刪除成果
bool result = false;
///斷定鍵值稱號和注冊表項稱號能否為空,假如為空,則前往false
if (name == string.Empty || name == null || subKey == string.Empty || subKey == null)
{
return false;
}
///斷定鍵值能否存在
if (IsRegeditKeyExist(name))
{
///以可寫方法翻開注冊表項
RegistryKey key = OpenSubKey(subKey, true);
if (key != null)
{
try
{
///刪除鍵值
key.DeleteValue(name);
result = true;
}
catch
{
result = false;
}
finally
{
///封閉對注冊表項的更改
key.Close();
}
}
}
return result;
}
/// <summary>
/// 刪除鍵值
/// </summary>
/// <param name="name">鍵值稱號</param>
/// <param name="subKey">注冊表項稱號</param>
/// <param name="regDomain">注冊表基項域</param>
/// <returns>假如刪除勝利,前往true,不然前往false</returns>
public virtual bool DeleteRegeditKey(string name, string subKey, RegDomain regDomain)
{
///刪除成果
bool result = false;
///斷定鍵值稱號和注冊表項稱號能否為空,假如為空,則前往false
if (name == string.Empty || name == null || subKey == string.Empty || subKey == null)
{
return false;
}
///斷定鍵值能否存在
if (IsRegeditKeyExist(name))
{
///以可寫方法翻開注冊表項
RegistryKey key = OpenSubKey(subKey, regDomain, true);
if (key != null)
{
try
{
///刪除鍵值
key.DeleteValue(name);
result = true;
}
catch
{
result = false;
}
finally
{
///封閉對注冊表項的更改
key.Close();
}
}
}
return result;
}
#endregion
#endregion
#region 受掩護辦法
/// <summary>
/// 獲得注冊表基項域對應頂級節點
/// 例子:如regDomain是ClassesRoot,則前往Registry.ClassesRoot
/// </summary>
/// <param name="regDomain">注冊表基項域</param>
/// <returns>注冊表基項域對應頂級節點</returns>
protected RegistryKey GetRegDomain(RegDomain regDomain)
{
///創立基於注冊表基項的節點
RegistryKey key;
#region 斷定注冊表基項域
switch (regDomain)
{
case RegDomain.ClassesRoot:
key = Registry.ClassesRoot; break;
case RegDomain.CurrentUser:
key = Registry.CurrentUser; break;
case RegDomain.LocalMachine:
key = Registry.LocalMachine; break;
case RegDomain.User:
key = Registry.Users; break;
case RegDomain.CurrentConfig:
key = Registry.CurrentConfig; break;
case RegDomain.DynDa:
key = Registry.DynData; break;
case RegDomain.PerformanceData:
key = Registry.PerformanceData; break;
default:
key = Registry.LocalMachine; break;
}
#endregion
return key;
}
/// <summary>
/// 獲得在注冊表中對應的值數據類型
/// 例子:如regValueKind是DWord,則前往RegistryValueKind.DWord
/// </summary>
/// <param name="regValueKind">注冊表數據類型</param>
/// <returns>注冊表中對應的數據類型</returns>
protected RegistryValueKind GetRegValueKind(RegValueKind regValueKind)
{
RegistryValueKind regValueK;
#region 斷定注冊表數據類型
switch (regValueKind)
{
case RegValueKind.Unknown:
regValueK = RegistryValueKind.Unknown; break;
case RegValueKind.String:
regValueK = RegistryValueKind.String; break;
case RegValueKind.ExpandString:
regValueK = RegistryValueKind.ExpandString; break;
case RegValueKind.Binary:
regValueK = RegistryValueKind.Binary; break;
case RegValueKind.DWord:
regValueK = RegistryValueKind.DWord; break;
case RegValueKind.MultiString:
regValueK = RegistryValueKind.MultiString; break;
case RegValueKind.QWord:
regValueK = RegistryValueKind.QWord; break;
default:
regValueK = RegistryValueKind.String; break;
}
#endregion
return regValueK;
}
#region 翻開注冊表項
/// <summary>
/// 翻開注冊表項節點,以只讀方法檢索子項
/// 虛辦法,子類可停止重寫
/// </summary>
/// <returns>假如SubKey為空、null或許SubKey指導注冊表項不存在,則前往null,不然前往注冊表節點</returns>
protected virtual RegistryKey OpenSubKey()
{
///斷定注冊表項稱號能否為空
if (_subkey == string.Empty || _subkey == null)
{
return null;
}
///創立基於注冊表基項的節點
RegistryKey key = GetRegDomain(_domain);
///要翻開的注冊表項的節點
RegistryKey sKey = null;
///翻開注冊表項
sKey = key.OpenSubKey(_subkey);
///封閉對注冊表項的更改
key.Close();
///前往注冊表節點
return sKey;
}
/// <summary>
/// 翻開注冊表項節點
/// 虛辦法,子類可停止重寫
/// </summary>
/// <param name="writable">假如須要項的寫拜訪權限,則設置為 true</param>
/// <returns>假如SubKey為空、null或許SubKey指導注冊表項不存在,則前往null,不然前往注冊表節點</returns>
protected virtual RegistryKey OpenSubKey(bool writable)
{
///斷定注冊表項稱號能否為空
if (_subkey == string.Empty || _subkey == null)
{
return null;
}
///創立基於注冊表基項的節點
RegistryKey key = GetRegDomain(_domain);
///要翻開的注冊表項的節點
RegistryKey sKey = null;
///翻開注冊表項
sKey = key.OpenSubKey(_subkey, writable);
///封閉對注冊表項的更改
key.Close();
///前往注冊表節點
return sKey;
}
/// <summary>
/// 翻開注冊表項節點,以只讀方法檢索子項
/// 虛辦法,子類可停止重寫
/// </summary>
/// <param name="subKey">注冊表項稱號</param>
/// <returns>假如SubKey為空、null或許SubKey指導注冊表項不存在,則前往null,不然前往注冊表節點</returns>
protected virtual RegistryKey OpenSubKey(string subKey)
{
///斷定注冊表項稱號能否為空
if (subKey == string.Empty || subKey == null)
{
return null;
}
///創立基於注冊表基項的節點
RegistryKey key = GetRegDomain(_domain);
///要翻開的注冊表項的節點
RegistryKey sKey = null;
///翻開注冊表項
sKey = key.OpenSubKey(subKey);
///封閉對注冊表項的更改
key.Close();
///前往注冊表節點
return sKey;
}
/// <summary>
/// 翻開注冊表項節點,以只讀方法檢索子項
/// 虛辦法,子類可停止重寫
/// </summary>
/// <param name="subKey">注冊表項稱號</param>
/// <param name="writable">假如須要項的寫拜訪權限,則設置為 true</param>
/// <returns>假如SubKey為空、null或許SubKey指導注冊表項不存在,則前往null,不然前往注冊表節點</returns>
protected virtual RegistryKey OpenSubKey(string subKey, bool writable)
{
///斷定注冊表項稱號能否為空
if (subKey == string.Empty || subKey == null)
{
return null;
}
///創立基於注冊表基項的節點
RegistryKey key = GetRegDomain(_domain);
///要翻開的注冊表項的節點
RegistryKey sKey = null;
///翻開注冊表項
sKey = key.OpenSubKey(subKey, writable);
///封閉對注冊表項的更改
key.Close();
///前往注冊表節點
return sKey;
}
/// <summary>
/// 翻開注冊表項節點,以只讀方法檢索子項
/// 虛辦法,子類可停止重寫
/// </summary>
/// <param name="subKey">注冊表項稱號</param>
/// <param name="regDomain">注冊表基項域</param>
/// <returns>假如SubKey為空、null或許SubKey指導注冊表項不存在,則前往null,不然前往注冊表節點</returns>
protected virtual RegistryKey OpenSubKey(string subKey, RegDomain regDomain)
{
///斷定注冊表項稱號能否為空
if (subKey == string.Empty || subKey == null)
{
return null;
}
///創立基於注冊表基項的節點
RegistryKey key = GetRegDomain(regDomain);
///要翻開的注冊表項的節點
RegistryKey sKey = null;
///翻開注冊表項
sKey = key.OpenSubKey(subKey);
///封閉對注冊表項的更改
key.Close();
///前往注冊表節點
return sKey;
}
/// <summary>
/// 翻開注冊表項節點
/// 虛辦法,子類可停止重寫
/// </summary>
/// <param name="subKey">注冊表項稱號</param>
/// <param name="regDomain">注冊表基項域</param>
/// <param name="writable">假如須要項的寫拜訪權限,則設置為 true</param>
/// <returns>假如SubKey為空、null或許SubKey指導注冊表項不存在,則前往null,不然前往注冊表節點</returns>
protected virtual RegistryKey OpenSubKey(string subKey, RegDomain regDomain, bool writable)
{
///斷定注冊表項稱號能否為空
if (subKey == string.Empty || subKey == null)
{
return null;
}
///創立基於注冊表基項的節點
RegistryKey key = GetRegDomain(regDomain);
///要翻開的注冊表項的節點
RegistryKey sKey = null;
///翻開注冊表項
sKey = key.OpenSubKey(subKey, writable);
///封閉對注冊表項的更改
key.Close();
///前往注冊表節點
return sKey;
}
#endregion
#endregion
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved