最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何在C#中使用注冊(cè)表

 更新時(shí)間:2020年12月31日 17:27:03   作者:Dwaynerbing  
這篇文章主要介紹了如何在C# 使用注冊(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ù)詳解

    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#中SqlTransaction——事務(wù)詳解

    c#中SqlTransaction——事務(wù)詳解

    這篇文章主要介紹了c#中SqlTransaction——事務(wù)詳解 ,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2016-12-12
  • C#引用類型作為方法的參數(shù)分析

    C#引用類型作為方法的參數(shù)分析

    這篇文章主要介紹了C#引用類型作為方法的參數(shù)分析,以實(shí)例的形式較為詳細(xì)的分析了參數(shù)的傳值問(wèn)題,需要的朋友可以參考下
    2014-11-11
  • 基于C#實(shí)現(xiàn)員工IC卡的讀寫(xiě)功能

    基于C#實(shí)現(xiàn)員工IC卡的讀寫(xiě)功能

    這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)讀寫(xiě)員工IC卡的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-01-01
  • C#自定義日志記錄

    C#自定義日志記錄

    這篇文章主要為大家詳細(xì)介紹了C#自定義日志記錄的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • C#七大經(jīng)典排序算法系列(下)

    C#七大經(jīng)典排序算法系列(下)

    這篇文章主要為大家詳細(xì)介紹了C#七大經(jīng)典排序算法系列下篇,直接插入排序,希爾排序和歸并排序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • C#商品管理系統(tǒng)簡(jiǎn)易版

    C#商品管理系統(tǒng)簡(jiǎn)易版

    這篇文章主要為大家詳細(xì)介紹了C#商品管理系統(tǒng)簡(jiǎn)易版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#使用csvhelper實(shí)現(xiàn)csv的基本操作

    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)始)

    C#+MO實(shí)現(xiàn)一個(gè)道路編輯軟件(剛開(kāi)始)...
    2007-04-04
  • C#如何自定義線性節(jié)點(diǎn)鏈表集合

    C#如何自定義線性節(jié)點(diǎn)鏈表集合

    C#如何自定義線性節(jié)點(diǎn)鏈表集合,這篇文章主要為大家詳細(xì)介紹了C#基于泛型的自定義線性節(jié)點(diǎn)鏈表集合示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評(píng)論

阳原县| 新竹县| 海南省| 怀安县| 大洼县| 当雄县| 北安市| 邢台县| 布尔津县| 山阴县| 陇川县| 蒙自县| 兴国县| 临夏市| 项城市| 肃北| 通山县| 安新县| 灌云县| 汤阴县| 宜良县| 霍林郭勒市| 县级市| 温州市| 循化| 荆门市| 漳浦县| 遂川县| 望奎县| 偃师市| 盱眙县| 中山市| 崇仁县| 孟连| 奉化市| 浦北县| 博罗县| 和静县| 曲阜市| 文化| 舟山市|