今天偶然看見一篇有修改注冊表的文章,心中便想C#是否可以修改注冊表,於是自己便從網上找了一些文章學習。
雖然學的很淺,但自己又進步了!
首先瞭解下C#操作注冊表的類
(1).Registry類:此類主要封裝了七個公有的靜態域,而這些靜態域分別代表這視窗註冊表中的七個基本的主鍵,具體如下所示:
Registry.ClassesRoot 對應於HKEY_CLASSES_ROOT主鍵
Registry.CurrentUser 對應於HKEY_CURRENT_USER主鍵
Registry.LocalMachine 對應於 HKEY_LOCAL_MACHINE主鍵
Registry.User 對應於 HKEY_USER主鍵
Registry.CurrentConfig 對應於HEKY_CURRENT_CONFIG主鍵
Registry.DynDa 對應於HKEY_DYN_DATA主鍵
Registry.PerformanceData 對應於HKEY_PERFORMANCE_DATA主鍵
(2).RegistryKey類:此類中主要封裝了對視窗系統註冊表的基本操作。在程式設計中,首先通過Registry類找到註冊表中的基本主鍵,然後通過RegistryKey類,來找其下面的子鍵和處理具體的操作的。
代碼: using Microsoft.Win32;
/// <summary>
///得到注冊表中的值
/// </summary>
/// <returns></returns>
private string GetRegistData( )
{
string registData;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey RootFolder = Registry.LocalMachine;
RegistryKey system = RootFolder.OpenSubKey("system",true);
RegistryKey CurrentControlSet = system.OpenSubKey("CurrentControlSet",true);
RegistryKey Services = CurrentControlSet.OpenSubKey("Services",true);
RegistryKey Tcpip = Services.OpenSubKey("Tcpip",true);
RegistryKey Parameters = Tcpip.OpenSubKey("Parameters",true);
RegistryKey NetCardID = Parameters.OpenSubKey("Winsock",true);
registData = NetCardID.GetValue("HelperDllName", "null").ToString();
return registData;
}
/// <summary>
/// 創建注冊表值
/// 本例為創建一個XX的子目錄,其創建一鍵為XXX,值為test
/// </summary>
private void CreateRegistData()
{
RegistryKey hkml = Registry.LocalMachine;
RegistryKey RootFolder = Registry.LocalMachine;
RegistryKey system = RootFolder.OpenSubKey("system", true);
RegistryKey CurrentControlSet = system.OpenSubKey("CurrentControlSet", true);
RegistryKey Services = CurrentControlSet.OpenSubKey("Services", true);
RegistryKey Tcpip = Services.OpenSubKey("Tcpip", true);
RegistryKey Parameters = Tcpip.OpenSubKey("Parameters", true);
RegistryKey NetCardID = Parameters.OpenSubKey("Winsock", true);
RegistryKey xxx= NetCardID.CreateSubKey("xxx");
//384為十進制,Dword為16進制,系統將自動進行轉換,最終注冊表存入的值為180,180為384的16進制
xxx.SetValue("xxx",384,RegistryValueKind.DWord);
}
/// <summary>
/// 刪除指的鍵
/// </summary>
private void DeleteValueRegistData()
{
RegistryKey hkml = Registry.LocalMachine;
RegistryKey RootFolder = Registry.LocalMachine;
RegistryKey system = RootFolder.OpenSubKey("system", true);
RegistryKey CurrentControlSet = system.OpenSubKey("CurrentControlSet", true);
RegistryKey Services = CurrentControlSet.OpenSubKey("Services", true);
RegistryKey Tcpip = Services.OpenSubKey("Tcpip", true);
RegistryKey Parameters = Tcpip.OpenSubKey("Parameters", true);
RegistryKey NetCardID = Parameters.OpenSubKey("Winsock", true);
RegistryKey xxx = NetCardID.OpenSubKey("xxx", true);
string[] SubValue;
SubValue = xxx.GetValueNames();
foreach (string sSubValue in SubValue)
{
if (sSubValue=="xxx")
{
xxx.DeleteValue(sSubValue);
}
}
}
/// <summary>
/// 刪除子目錄
/// </summary>
private void DeleteSubRegistData()
{
RegistryKey hkml = Registry.LocalMachine;
RegistryKey RootFolder = Registry.LocalMachine;
RegistryKey system = RootFolder.OpenSubKey("system", true);
RegistryKey CurrentControlSet = system.OpenSubKey("CurrentControlSet", true);
RegistryKey Services = CurrentControlSet.OpenSubKey("Services", true);
RegistryKey Tcpip = Services.OpenSubKey("Tcpip", true);
RegistryKey Parameters = Tcpip.OpenSubKey("Parameters", true);
RegistryKey NetCardID = Parameters.OpenSubKey("Winsock", true);
string[] SubValue;
SubValue = NetCardID.GetSubKeyNames();
foreach (string sSubValue in SubValue)
{
if (sSubValue == "xxx")
{
NetCardID.DeleteSubKey (sSubValue);
}
}
}