欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何在C#中使用注冊表

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

一、什么是注冊表

        注冊表是Microsoft Windows操作系統(tǒng)和其應(yīng)用程序中的一個重要的層次型數(shù)據(jù)庫,用于存儲系統(tǒng)和應(yīng)用程序的設(shè)置信息。由鍵(key,或稱“項”)、子鍵(subkey,子項)和值項(value)構(gòu)成。一個鍵就是樹狀數(shù)據(jù)結(jié)構(gòu)中的一個節(jié)點,而子鍵就是這個節(jié)點的子節(jié)點,子鍵也是鍵。一個值項則是一個鍵的一條屬性,由名稱(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 文件,實現(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)容,更多關(guān)于c# 使用注冊表的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論