如何在C#中使用登錄檔
阿新 • • 發佈:2021-01-04
一、什麼是登錄檔
登錄檔是Microsoft Windows作業系統和其應用程式中的一個重要的層次型資料庫,用於儲存系統和應用程式的設定資訊。由鍵(key,或稱“項”)、子鍵(subkey,子項)和值項(value)構成。一個鍵就是樹狀資料結構中的一個節點,而子鍵就是這個節點的子節點,子鍵也是鍵。一個值項則是一個鍵的一條屬性,由名稱(name)、資料型別(datatype)以及資料(data)組成。一個鍵可以有一個或多個值,每個值的名稱各不相同,如果一個值的名稱為空,則該值為該鍵的預設值。
(1)、登錄檔的資料型別主要有以下五種:
(2)、登錄檔有以下5個一級分支:
(3)、登錄檔的儲存方式:
Windows NT系列作業系統和Windows 9x系列的儲存方式有很大區別。登錄檔被分成多個檔案儲存,稱為Registry Hives,每一個檔案被稱為一個配置單元。在早期的Windows 3.x系列中,登錄檔僅包含一個reg.dat檔案,所存放的內容後來演變為HKEY_CLASSES_ROOT分支。
Windows NT家族的配置單元檔案:
Windows 9x家族的配置單元檔案:
二、C#操作登錄檔
我們可以使用外部載入windows作業系統自帶的Advapi32.dll 檔案,實現登錄檔的操作。下面例項中,我們使用 .NET 平臺自帶的Microsoft.Win32.Registry 類庫,使用的時候新增如下引用:
using Microsoft.Win32;
具體操作如下:
public static class RegistryUtil { #region 建立登錄檔 /// <summary> /// 建立登錄檔項 /// </summary> /// <param name="keyPath"></param> /// <param name="parentKey"></param> /// <returns></returns> public static RegistryKey CreateRegistryKey(string keyPath,RegistryKey parentKey) { return parentKey?.CreateSubKey(keyPath); } /// <summary> /// 建立<see cref="Registry.LocalMachine">登錄檔專案 /// </summary> /// <param name="keyPath"></param> /// <returns></returns> public static RegistryKey CreateRegistryLocalMachine64Key(string keyPath) { return Registry.LocalMachine.CreateSubKey(keyPath); } /// <summary> /// 建立<see cref="Registry.ClassesRoot">登錄檔專案 /// </summary> /// <param name="keyPath"></param> /// <returns></returns> public static RegistryKey CreateRegistryClassesRootKey(string keyPath) { return Registry.ClassesRoot.CreateSubKey(keyPath); } /// <summary> /// 建立<see cref="Registry.CurrentConfig">登錄檔專案 /// </summary> /// <param name="keyPath"></param> /// <returns></returns> public static RegistryKey CreateRegistryCurrentConfigKey(string keyPath) { return Registry.CurrentConfig.CreateSubKey(keyPath); } /// <summary> /// 建立<see cref="Registry.CurrentUser">登錄檔專案 /// </summary> /// <param name="keyPath"></param> /// <returns></returns> public static RegistryKey CreateRegistryCurrentUserKey(string keyPath) { return Registry.CurrentUser.CreateSubKey(keyPath); } /// <summary> /// 建立<see cref="Registry.PerformanceData">登錄檔專案 /// </summary> /// <param name="keyPath"></param> /// <returns></returns> public static RegistryKey CreateRegistryPerformanceDataKey(string keyPath) { return Registry.PerformanceData.CreateSubKey(keyPath); } /// <summary> /// 建立<see cref="Registry.Users">登錄檔專案 /// </summary> /// <param name="keyPath"></param> /// <returns></returns> public static RegistryKey CreateRegistryUsersKey(string keyPath) { return Registry.Users.CreateSubKey(keyPath); } #endregion #region 開啟登錄檔 /// <summary> /// 開啟 <see cref="RegistryView.Registry64"/> 的 <see cref="RegistryHive.LocalMachine"/> /// </summary> /// <returns></returns> public static RegistryKey OpenLocalMachine64Key() { return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine,RegistryView.Registry64); } /// <summary> /// 開啟 <see cref="RegistryView.Registry32"/> 的 <see cref="RegistryHive.LocalMachine"/> /// </summary> /// <returns></returns> public static RegistryKey OpenLocalMachine32Key() { return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine,RegistryView.Registry32); } /// <summary> /// 開啟 <see cref="RegistryView.Registry64"/> 的 <see cref="RegistryHive.CurrentUser"/> /// </summary> /// <returns></returns> public static RegistryKey OpenCurrentUser64Key() { return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser,RegistryView.Registry64); } /// <summary> /// 開啟 <see cref="RegistryView.Registry32"/> 的 <see cref="RegistryHive.LocalMachine"/> /// </summary> /// <returns></returns> public static RegistryKey OpenCurrentUser32Key() { return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser,RegistryView.Registry32); } /// <summary> /// 開啟登錄檔鍵 /// </summary> /// <param name="rootKey"></param> /// <param name="keyPath"></param> /// <returns></returns> public static RegistryKey OpenKey(this RegistryKey rootKey,string keyPath) { return rootKey.OpenSubKey(keyPath,true); } #endregion /// <summary> /// 讀取登錄檔值 /// </summary> /// <param name="key"></param> /// <param name="name"></param> /// <returns></returns> public static string ReadRegistryValue(this RegistryKey key,string name) { return key?.GetValue(name)?.ToString(); } /// <summary> /// 寫入登錄檔值 /// </summary> /// <param name="key"></param> /// <param name="name"></param> /// <param name="value"></param> public static void WriteRegistryValue(this RegistryKey key,string name,string value) { key.SetValue(name,value); } /// <summary> /// 刪除註冊值 /// </summary> /// <param name="key"></param> /// <param name="name"></param> public static void DeleteRegistryValue(this RegistryKey key,string name) { key.DeleteValue(name); } }
除錯程式碼如下:
static void Main(string[] args) { // 寫入登錄檔 string path = @"Software\Test\Char"; var key = RegistryUtil.CreateRegistryLocalMachine64Key(path); key.WriteRegistryValue("Name","Dwayne"); key.WriteRegistryValue("Age","18"); // 讀取登錄檔 var regKey = RegistryUtil.OpenLocalMachine64Key().OpenKey(path); var name = regKey.ReadRegistryValue("Name"); var age = regKey.ReadRegistryValue("Age"); Console.WriteLine($"Name={name},Age={age}"); Console.WriteLine("Hello World!"); }
以上就是如何在C# 中使用登錄檔的詳細內容,更多關於c# 使用登錄檔的資料請關注我們其它相關文章!