如何在C#中使用注冊(cè)表
一、什么是注冊(cè)表
注冊(cè)表是Microsoft Windows操作系統(tǒng)和其應(yīng)用程序中的一個(gè)重要的層次型數(shù)據(jù)庫(kù),用于存儲(chǔ)系統(tǒng)和應(yīng)用程序的設(shè)置信息。由鍵(key,或稱“項(xiàng)”)、子鍵(subkey,子項(xiàng))和值項(xiàng)(value)構(gòu)成。一個(gè)鍵就是樹(shù)狀數(shù)據(jù)結(jié)構(gòu)中的一個(gè)節(jié)點(diǎn),而子鍵就是這個(gè)節(jié)點(diǎn)的子節(jié)點(diǎn),子鍵也是鍵。一個(gè)值項(xiàng)則是一個(gè)鍵的一條屬性,由名稱(name)、數(shù)據(jù)類型(datatype)以及數(shù)據(jù)(data)組成。一個(gè)鍵可以有一個(gè)或多個(gè)值,每個(gè)值的名稱各不相同,如果一個(gè)值的名稱為空,則該值為該鍵的默認(rèn)值。
(1)、注冊(cè)表的數(shù)據(jù)類型主要有以下五種:

(2)、注冊(cè)表有以下5個(gè)一級(jí)分支:

(3)、注冊(cè)表的存儲(chǔ)方式:
Windows NT系列操作系統(tǒng)和Windows 9x系列的存儲(chǔ)方式有很大區(qū)別。注冊(cè)表被分成多個(gè)文件存儲(chǔ),稱為Registry Hives,每一個(gè)文件被稱為一個(gè)配置單元。在早期的Windows 3.x系列中,注冊(cè)表僅包含一個(gè)reg.dat文件,所存放的內(nèi)容后來(lái)演變?yōu)镠KEY_CLASSES_ROOT分支。
Windows NT家族的配置單元文件:

Windows 9x家族的配置單元文件:

二、C#操作注冊(cè)表
我們可以使用外部加載windows操作系統(tǒng)自帶的 Advapi32.dll 文件,實(shí)現(xiàn)注冊(cè)表的操作。下面實(shí)例中,我們使用 .NET 平臺(tái)自帶的 Microsoft.Win32.Registry 類庫(kù),使用的時(shí)候添加如下引用:
using Microsoft.Win32;
具體操作如下:
public static class RegistryUtil
{
#region 創(chuàng)建注冊(cè)表
/// <summary>
/// 創(chuàng)建注冊(cè)表項(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">注冊(cè)表項(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">注冊(cè)表項(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">注冊(cè)表項(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">注冊(cè)表項(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">注冊(cè)表項(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">注冊(cè)表項(xiàng)目
/// </summary>
/// <param name="keyPath"></param>
/// <returns></returns>
public static RegistryKey CreateRegistryUsersKey(string keyPath)
{
return Registry.Users.CreateSubKey(keyPath);
}
#endregion
#region 打開(kāi)注冊(cè)表
/// <summary>
/// 打開(kāi) <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>
/// 打開(kāi) <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>
/// 打開(kāi) <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>
/// 打開(kāi) <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>
/// 打開(kāi)注冊(cè)表鍵
/// </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>
/// 讀取注冊(cè)表值
/// </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>
/// 寫(xiě)入注冊(cè)表值
/// </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>
/// 刪除注冊(cè)值
/// </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)
{
// 寫(xiě)入注冊(cè)表
string path = @"Software\Test\Char";
var key = RegistryUtil.CreateRegistryLocalMachine64Key(path);
key.WriteRegistryValue("Name", "Dwayne");
key.WriteRegistryValue("Age", "18");
// 讀取注冊(cè)表
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è)表的詳細(xì)內(nèi)容,更多關(guān)于c# 使用注冊(cè)表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#數(shù)據(jù)庫(kù)操作之LINQ to SQL技術(shù)詳解
本文詳細(xì)介紹了LINQtoSQL技術(shù),包括其基本概念、使用方法、動(dòng)態(tài)創(chuàng)建數(shù)據(jù)庫(kù)、插入數(shù)據(jù)、查詢數(shù)據(jù)、更新數(shù)據(jù)和刪除數(shù)據(jù)等操作2024-12-12
基于C#實(shí)現(xiàn)員工IC卡的讀寫(xiě)功能
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)讀寫(xiě)員工IC卡的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2023-01-01
C#使用csvhelper實(shí)現(xiàn)csv的基本操作
CsvHelper 是一個(gè)用于讀寫(xiě) CSV 文件的.NET庫(kù),極其快速,靈活且易于使用,CsvHelper 建立在.NET Standard 2.0 之上,幾乎可以在任何地方運(yùn)行,本文給大家介紹了C#使用csvhelper實(shí)現(xiàn)csv的基本操作,需要的朋友可以參考下2024-07-07
C#+MO實(shí)現(xiàn)一個(gè)道路編輯軟件(剛開(kāi)始)
C#+MO實(shí)現(xiàn)一個(gè)道路編輯軟件(剛開(kāi)始)...2007-04-04

