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

如何在C#中使用注冊表

 更新時間:2020年12月31日 17:27:03   作者:Dwaynerbing  
這篇文章主要介紹了如何在C# 使用注冊表,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下

一、什么是注冊表

        注冊表是Microsoft Windows操作系統(tǒng)和其應用程序中的一個重要的層次型數(shù)據(jù)庫,用于存儲系統(tǒng)和應用程序的設置信息。由鍵(key,或稱“項”)、子鍵(subkey,子項)和值項(value)構(gòu)成。一個鍵就是樹狀數(shù)據(jù)結(jié)構(gòu)中的一個節(jié)點,而子鍵就是這個節(jié)點的子節(jié)點,子鍵也是鍵。一個值項則是一個鍵的一條屬性,由名稱(name)、數(shù)據(jù)類型(datatype)以及數(shù)據(jù)(data)組成。一個鍵可以有一個或多個值,每個值的名稱各不相同,如果一個值的名稱為空,則該值為該鍵的默認值。

     (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 文件,實現(xiàn)注冊表的操作。下面實例中,我們使用 .NET 平臺自帶的 Microsoft.Win32.Registry 類庫,使用的時候添加如下引用:

using Microsoft.Win32;

具體操作如下:

public static class RegistryUtil
  {
    #region 創(chuàng)建注冊表
    /// <summary>
    /// 創(chuà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">注冊表項目
    /// </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">注冊表項目
    /// </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">注冊表項目
    /// </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">注冊表項目
    /// </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">注冊表項目
    /// </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">注冊表項目
    /// </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# 中使用注冊表的詳細內(nèi)容,更多關于c# 使用注冊表的資料請關注腳本之家其它相關文章!

相關文章

  • C#數(shù)據(jù)庫操作之LINQ to SQL技術(shù)詳解

    C#數(shù)據(jù)庫操作之LINQ to SQL技術(shù)詳解

    本文詳細介紹了LINQtoSQL技術(shù),包括其基本概念、使用方法、動態(tài)創(chuàng)建數(shù)據(jù)庫、插入數(shù)據(jù)、查詢數(shù)據(jù)、更新數(shù)據(jù)和刪除數(shù)據(jù)等操作
    2024-12-12
  • c#中SqlTransaction——事務詳解

    c#中SqlTransaction——事務詳解

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

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

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

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

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

    C#自定義日志記錄

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

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

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

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

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

    C#使用csvhelper實現(xiàn)csv的基本操作

    CsvHelper 是一個用于讀寫 CSV 文件的.NET庫,極其快速,靈活且易于使用,CsvHelper 建立在.NET Standard 2.0 之上,幾乎可以在任何地方運行,本文給大家介紹了C#使用csvhelper實現(xiàn)csv的基本操作,需要的朋友可以參考下
    2024-07-07
  • C#+MO實現(xiàn)一個道路編輯軟件(剛開始)

    C#+MO實現(xiàn)一個道路編輯軟件(剛開始)

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

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

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

最新評論

工布江达县| 万源市| 永修县| 麻城市| 高要市| 双鸭山市| 武清区| 黄龙县| 社旗县| 昭平县| 兴安县| 承德县| 平塘县| 商洛市| 长宁县| 甘肃省| 麻江县| 微博| 林周县| 吕梁市| 香格里拉县| 奉节县| 英德市| 松阳县| 鄂托克旗| 武宁县| 阿巴嘎旗| 砚山县| 黄平县| 全椒县| 奇台县| 望城县| 房产| 东方市| 桐乡市| 喀什市| 夏邑县| 留坝县| 永州市| 土默特右旗| 抚顺县|