1. 程式人生 > 實用技巧 >C# 使用登錄檔

C# 使用登錄檔

一、什麼是登錄檔

登錄檔是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;
View Code

具體操作如下:

  1  public static class RegistryUtil
  2     {
  3         #region 建立登錄檔
  4         /// <summary>
  5         /// 建立登錄檔項
  6         /// </summary>
  7         /// <param name="keyPath"></param>
  8         /// <param name="parentKey"></param>
9 /// <returns></returns> 10 public static RegistryKey CreateRegistryKey(string keyPath, RegistryKey parentKey) 11 { 12 return parentKey?.CreateSubKey(keyPath); 13 } 14 15 16 /// <summary> 17 /// 建立<see cref="Registry.LocalMachine">登錄檔專案 18 /// </summary> 19 /// <param name="keyPath"></param> 20 /// <returns></returns> 21 public static RegistryKey CreateRegistryLocalMachine64Key(string keyPath) 22 { 23 return Registry.LocalMachine.CreateSubKey(keyPath); 24 } 25 26 /// <summary> 27 /// 建立<see cref="Registry.ClassesRoot">登錄檔專案 28 /// </summary> 29 /// <param name="keyPath"></param> 30 /// <returns></returns> 31 public static RegistryKey CreateRegistryClassesRootKey(string keyPath) 32 { 33 return Registry.ClassesRoot.CreateSubKey(keyPath); 34 } 35 36 /// <summary> 37 /// 建立<see cref="Registry.CurrentConfig">登錄檔專案 38 /// </summary> 39 /// <param name="keyPath"></param> 40 /// <returns></returns> 41 public static RegistryKey CreateRegistryCurrentConfigKey(string keyPath) 42 { 43 return Registry.CurrentConfig.CreateSubKey(keyPath); 44 } 45 46 /// <summary> 47 /// 建立<see cref="Registry.CurrentUser">登錄檔專案 48 /// </summary> 49 /// <param name="keyPath"></param> 50 /// <returns></returns> 51 public static RegistryKey CreateRegistryCurrentUserKey(string keyPath) 52 { 53 return Registry.CurrentUser.CreateSubKey(keyPath); 54 } 55 56 /// <summary> 57 /// 建立<see cref="Registry.PerformanceData">登錄檔專案 58 /// </summary> 59 /// <param name="keyPath"></param> 60 /// <returns></returns> 61 public static RegistryKey CreateRegistryPerformanceDataKey(string keyPath) 62 { 63 return Registry.PerformanceData.CreateSubKey(keyPath); 64 } 65 66 /// <summary> 67 /// 建立<see cref="Registry.Users">登錄檔專案 68 /// </summary> 69 /// <param name="keyPath"></param> 70 /// <returns></returns> 71 public static RegistryKey CreateRegistryUsersKey(string keyPath) 72 { 73 return Registry.Users.CreateSubKey(keyPath); 74 } 75 #endregion 76 77 78 #region 開啟登錄檔 79 /// <summary> 80 /// 開啟 <see cref="RegistryView.Registry64"/><see cref="RegistryHive.LocalMachine"/> 81 /// </summary> 82 /// <returns></returns> 83 public static RegistryKey OpenLocalMachine64Key() 84 { 85 return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64); 86 } 87 88 /// <summary> 89 /// 開啟 <see cref="RegistryView.Registry32"/><see cref="RegistryHive.LocalMachine"/> 90 /// </summary> 91 /// <returns></returns> 92 public static RegistryKey OpenLocalMachine32Key() 93 { 94 return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32); 95 } 96 97 /// <summary> 98 /// 開啟 <see cref="RegistryView.Registry64"/><see cref="RegistryHive.CurrentUser"/> 99 /// </summary> 100 /// <returns></returns> 101 public static RegistryKey OpenCurrentUser64Key() 102 { 103 return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry64); 104 } 105 106 /// <summary> 107 /// 開啟 <see cref="RegistryView.Registry32"/><see cref="RegistryHive.LocalMachine"/> 108 /// </summary> 109 /// <returns></returns> 110 public static RegistryKey OpenCurrentUser32Key() 111 { 112 return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry32); 113 } 114 115 /// <summary> 116 /// 開啟登錄檔鍵 117 /// </summary> 118 /// <param name="rootKey"></param> 119 /// <param name="keyPath"></param> 120 /// <returns></returns> 121 public static RegistryKey OpenKey(this RegistryKey rootKey, string keyPath) 122 { 123 return rootKey.OpenSubKey(keyPath, true); 124 } 125 #endregion 126 127 128 /// <summary> 129 /// 讀取登錄檔值 130 /// </summary> 131 /// <param name="key"></param> 132 /// <param name="name"></param> 133 /// <returns></returns> 134 public static string ReadRegistryValue(this RegistryKey key, string name) 135 { 136 return key?.GetValue(name)?.ToString(); 137 } 138 139 /// <summary> 140 /// 寫入登錄檔值 141 /// </summary> 142 /// <param name="key"></param> 143 /// <param name="name"></param> 144 /// <param name="value"></param> 145 public static void WriteRegistryValue(this RegistryKey key, string name, string value) 146 { 147 key.SetValue(name, value); 148 } 149 150 /// <summary> 151 /// 刪除註冊值 152 /// </summary> 153 /// <param name="key"></param> 154 /// <param name="name"></param> 155 public static void DeleteRegistryValue(this RegistryKey key, string name) 156 { 157 key.DeleteValue(name); 158 } 159 }
View Code

除錯程式碼如下:

 1   static void Main(string[] args)
 2         {
 3             // 寫入登錄檔
 4             string path = @"Software\Test\Char";
 5             var key = RegistryUtil.CreateRegistryLocalMachine64Key(path);
 6             key.WriteRegistryValue("Name", "Dwayne");
 7             key.WriteRegistryValue("Age", "18");
 8 
 9             // 讀取登錄檔
10             var regKey = RegistryUtil.OpenLocalMachine64Key().OpenKey(path);
11             var name   = regKey.ReadRegistryValue("Name");
12             var age    = regKey.ReadRegistryValue("Age");
13             Console.WriteLine($"Name={name},Age={age}");
14 
15             Console.WriteLine("Hello World!");
16         }
View Code