当前位置 : 主页 > 网络编程 > c#编程 >

如何在C#中使用注册表

来源:互联网 收集:自由互联 发布时间:2021-05-09
一、什么是注册表 注册表是Microsoft Windows操作系统和其应用程序中的一个重要的层次型数据库,用于存储系统和应用程序的设置信息。由键(key,或称“项”)、子键(subkey,子项)和

一、什么是注册表

        注册表是Microsoft Windows操作系统和其应用程序中的一个重要的层次型数据库,用于存储系统和应用程序的设置信息。由键(key,或称“项”)、子键(subkey,子项)和值项(value)构成。一个键就是树状数据结构中的一个节点,而子键就是这个节点的子节点,子键也是键。一个值项则是一个键的一条属性,由名称(name)、数据类型(datatype)以及数据(data)组成。一个键可以有一个或多个值,每个值的名称各不相同,如果一个值的名称为空,则该值为该键的默认值。

     (1)、注册表的数据类型主要有以下五种:

   (2)、注册表有以下5个一级分支:

(3)、注册表的存储方式:

           Windows NT系列操作系统和Windows 9x系列的存储方式有很大区别。注册表被分成多个文件存储,称为Registry Hives,每一个文件被称为一个配置单元。在早期的Windows 3.x系列中,注册表仅包含一个reg.dat文件,所存放的内容后来演变为HKEY_CLASSES_ROOT分支。

Windows NT家族的配置单元文件:

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

 二、C#操作注册表

        我们可以使用外部加载windows操作系统自带的 Advapi32.dll 文件,实现注册表的操作。下面实例中,我们使用 .NET 平台自带的 Microsoft.Win32.Registry 类库,使用的时候添加如下引用:

using Microsoft.Win32;

具体操作如下:

public static class RegistryUtil
  {
    #region 创建注册表
    /// <summary>
    /// 创建注册表项
    /// </summary>
    /// <param name="keyPath"></param>
    /// <param name="parentKey"></param>
    /// <returns></returns>
    public static RegistryKey CreateRegistryKey(string keyPath, RegistryKey parentKey)
    {
      return parentKey?.CreateSubKey(keyPath);
    }


    /// <summary>
    /// 创建<see cref="Registry.LocalMachine">注册表项目
    /// </summary>
    /// <param name="keyPath"></param>
    /// <returns></returns>
    public static RegistryKey CreateRegistryLocalMachine64Key(string keyPath)
    {
      return Registry.LocalMachine.CreateSubKey(keyPath);
    }

    /// <summary>
    /// 创建<see cref="Registry.ClassesRoot">注册表项目
    /// </summary>
    /// <param name="keyPath"></param>
    /// <returns></returns>
    public static RegistryKey CreateRegistryClassesRootKey(string keyPath)
    {
      return Registry.ClassesRoot.CreateSubKey(keyPath);
    }

    /// <summary>
    /// 创建<see cref="Registry.CurrentConfig">注册表项目
    /// </summary>
    /// <param name="keyPath"></param>
    /// <returns></returns>
    public static RegistryKey CreateRegistryCurrentConfigKey(string keyPath)
    {
      return Registry.CurrentConfig.CreateSubKey(keyPath);
    }

    /// <summary>
    /// 创建<see cref="Registry.CurrentUser">注册表项目
    /// </summary>
    /// <param name="keyPath"></param>
    /// <returns></returns>
    public static RegistryKey CreateRegistryCurrentUserKey(string keyPath)
    {
      return Registry.CurrentUser.CreateSubKey(keyPath);
    }

    /// <summary>
    /// 创建<see cref="Registry.PerformanceData">注册表项目
    /// </summary>
    /// <param name="keyPath"></param>
    /// <returns></returns>
    public static RegistryKey CreateRegistryPerformanceDataKey(string keyPath)
    {
      return Registry.PerformanceData.CreateSubKey(keyPath);
    }

    /// <summary>
    /// 创建<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);
    }
  }

调试代码如下:

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# 中使用注册表的详细内容,更多关于c# 使用注册表的资料请关注自由互联其它相关文章!

网友评论