如何在C#中使用注冊表
一、什么是注冊表
注冊表是Microsoft Windows操作系統(tǒng)和其應(yīng)用程序中的一個重要的層次型數(shù)據(jù)庫,用于存儲系統(tǒng)和應(yīng)用程序的設(shè)置信息。由鍵(key,或稱“項(xiàng)”)、子鍵(subkey,子項(xiàng))和值項(xiàng)(value)構(gòu)成。一個鍵就是樹狀數(shù)據(jù)結(jié)構(gòu)中的一個節(jié)點(diǎn),而子鍵就是這個節(jié)點(diǎn)的子節(jié)點(diǎn),子鍵也是鍵。一個值項(xiàng)則是一個鍵的一條屬性,由名稱(name)、數(shù)據(jù)類型(datatype)以及數(shù)據(jù)(data)組成。一個鍵可以有一個或多個值,每個值的名稱各不相同,如果一個值的名稱為空,則該值為該鍵的默認(rèn)值。
(1)、注冊表的數(shù)據(jù)類型主要有以下五種:
(2)、注冊表有以下5個一級分支:
(3)、注冊表的存儲方式:
Windows NT系列操作系統(tǒng)和Windows 9x系列的存儲方式有很大區(qū)別。注冊表被分成多個文件存儲,稱為Registry Hives,每一個文件被稱為一個配置單元。在早期的Windows 3.x系列中,注冊表僅包含一個reg.dat文件,所存放的內(nèi)容后來演變?yōu)镠KEY_CLASSES_ROOT分支。
Windows NT家族的配置單元文件:
Windows 9x家族的配置單元文件:
二、C#操作注冊表
我們可以使用外部加載windows操作系統(tǒng)自帶的 Advapi32.dll 文件,實(shí)現(xiàn)注冊表的操作。下面實(shí)例中,我們使用 .NET 平臺自帶的 Microsoft.Win32.Registry 類庫,使用的時(shí)候添加如下引用:
using Microsoft.Win32;
具體操作如下:
public static class RegistryUtil { #region 創(chuàng)建注冊表 /// <summary> /// 創(chuàng)建注冊表項(xiàng) /// </summary> /// <param name="keyPath"></param> /// <param name="parentKey"></param> /// <returns></returns> public static RegistryKey CreateRegistryKey(string keyPath, RegistryKey parentKey) { return parentKey?.CreateSubKey(keyPath); } /// <summary> /// 創(chuàng)建<see cref="Registry.LocalMachine">注冊表項(xiàng)目 /// </summary> /// <param name="keyPath"></param> /// <returns></returns> public static RegistryKey CreateRegistryLocalMachine64Key(string keyPath) { return Registry.LocalMachine.CreateSubKey(keyPath); } /// <summary> /// 創(chuàng)建<see cref="Registry.ClassesRoot">注冊表項(xiàng)目 /// </summary> /// <param name="keyPath"></param> /// <returns></returns> public static RegistryKey CreateRegistryClassesRootKey(string keyPath) { return Registry.ClassesRoot.CreateSubKey(keyPath); } /// <summary> /// 創(chuàng)建<see cref="Registry.CurrentConfig">注冊表項(xiàng)目 /// </summary> /// <param name="keyPath"></param> /// <returns></returns> public static RegistryKey CreateRegistryCurrentConfigKey(string keyPath) { return Registry.CurrentConfig.CreateSubKey(keyPath); } /// <summary> /// 創(chuàng)建<see cref="Registry.CurrentUser">注冊表項(xiàng)目 /// </summary> /// <param name="keyPath"></param> /// <returns></returns> public static RegistryKey CreateRegistryCurrentUserKey(string keyPath) { return Registry.CurrentUser.CreateSubKey(keyPath); } /// <summary> /// 創(chuàng)建<see cref="Registry.PerformanceData">注冊表項(xiàng)目 /// </summary> /// <param name="keyPath"></param> /// <returns></returns> public static RegistryKey CreateRegistryPerformanceDataKey(string keyPath) { return Registry.PerformanceData.CreateSubKey(keyPath); } /// <summary> /// 創(chuàng)建<see cref="Registry.Users">注冊表項(xiàng)目 /// </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); } }
調(diào)試代碼如下:
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# 中使用注冊表的詳細(xì)內(nèi)容,更多關(guān)于c# 使用注冊表的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# 9 新特性——record的相關(guān)總結(jié)
這篇文章主要介紹了C# 9 新特性——record的相關(guān)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用c# 9的新特性,感興趣的朋友可以了解下2021-02-02C#語言基礎(chǔ)——結(jié)構(gòu)體和枚舉類型全面解析
下面小編就為大家?guī)硪黄狢#語言基礎(chǔ)——結(jié)構(gòu)體和枚舉類型全面解析。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07C#日期格式字符串的相互轉(zhuǎn)換操作實(shí)例分析
這篇文章主要介紹了C#日期格式字符串的相互轉(zhuǎn)換操作,結(jié)合實(shí)例形式分析了C#日期格式字符串的相互轉(zhuǎn)換操作函數(shù)與相關(guān)使用技巧,需要的朋友可以參考下2019-08-08C#操作SQLite數(shù)據(jù)庫方法小結(jié)(創(chuàng)建,連接,插入,查詢,刪除等)
這篇文章主要介紹了C#操作SQLite數(shù)據(jù)庫方法,包括針對SQLite數(shù)據(jù)庫的創(chuàng)建,連接,插入,查詢,刪除等操作,并提供了一個SQLite的封裝類,需要的朋友可以參考下2016-07-07C#使用IronPython庫調(diào)用Python腳本
這篇文章介紹了C#使用IronPython庫調(diào)用Python腳本的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06C#利用Label標(biāo)簽控件模擬窗體標(biāo)題的移動及窗體顏色不斷變換效果
Label標(biāo)簽控件相信對大家來說都不陌生,下面這篇文章主要給大家介紹了關(guān)于C#利用Label標(biāo)簽控件模擬窗體標(biāo)題的移動及窗體顏色不斷變換效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12