Unity 注册表操作

本文详细介绍了如何在Unity项目中使用Registry和RegistryKey类进行注册表操作,包括创建、读取、写入和删除键值,以及示例代码。特别强调了在Unity中设置API兼容级别以支持Registry操作。
摘要由CSDN通过智能技术生成

内容将会持续更新,有错误的地方欢迎指正,谢谢!
 

Unity 注册表操作
     
TechX 坚持将创新的科技带给世界!

拥有更好的学习体验 —— 不断努力,不断进步,不断探索
TechX —— 心探索、心进取!

助力快速掌握 Registry注册表操作

为初学者节省宝贵的学习时间,避免困惑!


前言:

  在Unity项目中,有的时候需要对注册表进行操作,今天这边博客将带领你学会怎么在Unity中对注册表进行增删改查操作。

在进行注册表操作之前需要对Unity进行一下设置,API兼容级别切换成“.NET Framework”。

在这里插入图片描述



一、Registry类


Registry 类是一个静态类,提供了用于访问注册表顶级节点的方法。它包含常用的注册表根节点,例如 CurrentUser、LocalMachine、ClassesRoot、CurrentConfig 等。通过这些静态属性,可以获取到不同根节点的 RegistryKey 对象,从而进一步操作注册表的子项和值。

在这里插入图片描述

ClassesRoot提供对 HKEY_CLASSES_ROOT 键的访问。
CurrentConfig提供对 HKEY_CURRENT_CONFIG 键的访问。
CurrentUser提供对 HKEY_CURRENT_USER 键的访问。
LocalMachine提供对 HKEY_LOCAL_MACHINE 键的访问。
Users提供对 HKEY_LOCAL_MACHINE 键的访问。

在这里插入图片描述




二、RegistryKey类


RegistryKey 类表示注册表中的一个键或子项。通过 Registry 类或者 RegistryKey 实例的方法可以打开、创建、删除子项,以及读取、设置、删除键值等操作。

在这里插入图片描述

Name获取注册表项的名称。
SubKeyCount获取该项下的子项数目。
ValueCount获取该项下的值的数目。
CreateSubKey(string subkey) 创建一个指定名称的子项或打开一个已有的子项。
OpenSubKey(string name) 打开注册表中指定子项的方法。
DeleteSubKey(string subkey)删除指定的子项。
SetValue(string name, object value)将指定名称的值设置为指定的数据。
GetValue(string name)检索指定名称的值。
GetSubKeyNames()检索当前键下所有子项的名称。
GetValueNames()检索当前键下所有值的名称。


三、注册表操作


using Microsoft.Win32;
using System.Text;

namespace TrialManager
{
    public class RegistryManager
    {
        /// <summary>
        /// 向注册表中写入值
        /// </summary>
        /// <param name="softName"></param>
        /// <param name="subKey"></param>
        /// <param name="valueName"></param>
        /// <param name="value"></param>
        public static void SetRegistryKey(string softName, string subKey, string valueName, string value)
        {
            //选择要操作的大项
            RegistryKey key = Registry.CurrentUser;

            RegistryKey software = key.OpenSubKey("SOFTWARE", true);

            RegistryKey softKey = software.OpenSubKey(softName, true);

            if (softKey == null)
            {
                softKey = software.CreateSubKey(softName, true);
            }

            RegistryKey versionKey = softKey.OpenSubKey(subKey, true);

            if (versionKey == null)
            {
                versionKey = softKey.CreateSubKey(subKey, true);
            }

            byte[] data = Encoding.UTF8.GetBytes(value);

            //在项里创建值
            versionKey.SetValue(valueName, data, RegistryValueKind.Binary);

            //最后要关掉
            versionKey.Close();
        }

        /// <summary>
        /// 获取注册表中的值
        /// </summary>
        /// <param name="softName"></param>
        /// <param name="subKey"></param>
        /// <param name="valueName"></param>
        /// <returns></returns>
        public static string GetRegistData(string softName, string subKey, string valueName)
        {
            string registData = null;
            RegistryKey key = Registry.CurrentUser;

            RegistryKey software = key.OpenSubKey("SOFTWARE", true);

            RegistryKey softKey = software.OpenSubKey(softName, true);

            if (softKey != null)
            {
                RegistryKey versionKey = softKey.OpenSubKey(subKey, true);

                if (versionKey != null)
                {
                    // 读取二进制数据
                    byte[] binaryData = (byte[])versionKey.GetValue(valueName, null);

                    if (binaryData != null)
                    {
                        // 将二进制数据转换为字符串
                        registData = Encoding.UTF8.GetString(binaryData);
                    }
                }
            }

            return registData;
        }

        /// <summary>
        /// 删除注册表
        /// </summary>
        /// <param name="softName"></param>

        public static void DeletRegistKey(string softName)
        {
            RegistryKey key = Registry.CurrentUser;

            RegistryKey software = key.OpenSubKey("SOFTWARE", true);

            RegistryKey softKey = software.OpenSubKey(softName, true);

            if (softKey != null)
            {
                software.DeleteSubKey(softName);
            }
        }

        /// <summary>
        /// 删除注册表
        /// </summary>
        /// <param name="softName"></param>
        /// <param name="subKey"></param>
        public static void DeletRegistKey(string softName, string subKey)
        {
            RegistryKey key = Registry.CurrentUser;

            RegistryKey software = key.OpenSubKey("SOFTWARE", true);

            RegistryKey softKey = software.OpenSubKey(softName, true);

            if (softKey != null)
            {
                RegistryKey versionKey = softKey.OpenSubKey(subKey, true);

                if (versionKey != null)
                {
                    softKey.DeleteSubKey(subKey);
                }
            }
        }

        /// <summary>
        /// 向注册表里面写入值
        /// </summary>
        /// <param name="softName"></param>
        /// <param name="valueName"></param>
        /// <param name="data"></param>
        public static void SetRegistryKey(string softName, string valueName, byte[] data)
        {
            //选择要操作的大项
            RegistryKey key = Registry.CurrentUser;

            RegistryKey software = key.OpenSubKey("SOFTWARE", true);

            RegistryKey valueKey = software.OpenSubKey(softName, true);

            if (valueKey == null)
            {
                valueKey = software.CreateSubKey(softName, true);
            }

            //在项里创建值
            valueKey.SetValue(valueName, data, RegistryValueKind.Binary);

            //最后要关掉
            valueKey.Close();
        }

        /// <summary>
        /// 检查注册表中是否存在特定名称的子项
        /// </summary>
        /// <param name="softName"></param>
        /// <param name="valueName"></param>
        /// <returns></returns>
        public static bool IsRegeditExit(string softName, string valueName)
        {
            bool _exit = false;
            string[] subkeyNames;

            RegistryKey hkml = Registry.CurrentUser;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.OpenSubKey(softName, true);
            if (aimdir == null)
            {
                return false;
            }
            subkeyNames = aimdir.GetSubKeyNames();

            foreach (string keyName in subkeyNames)
            {
                if (keyName == valueName)
                {
                    _exit = true;
                    return _exit;
                }
            }
            return _exit;
        }
    }
}
  • SetRegistryKey(string softName, string subKey, string valueName, string value):

    在注册表中设置键值对,指定了软件名称、子项名称、值名称和值内容。它会检查并创建相应的注册表子项,然后将给定值写入其中。

  • GetRegistData(string softName, string subKey, string valueName):

    根据给定的软件名称、子项名称和值名称从注册表中获取值数据。它读取注册表中的二进制数据并转换为字符串返回。

  • DeletRegistKey(string softName) 和 DeletRegistKey(string softName, string subKey):

    用于删除注册表中的子项或特定子项下的特定键。可以根据提供的软件名称或软件名称和子项名称来删除相应的注册表键。

  • SetRegistryKey(string softName, string valueName, byte[] data):

    类似于 SetRegistryKey 方法,但接受二进制数据。

  • IsRegeditExit(string softName, string valueName):

    检查注册表中是否存在特定名称的子项。

这些方法通过使用 Microsoft.Win32 中的 Registry 和 RegistryKey 类来操作 Windows 注册表,执行创建、读取、写入和删除注册表项及其值的操作。



四、注册表示例


public class MyComponent1 : MonoBehaviour
{
    private void Start()
    {
        RegistryManager.SetRegistryKey("App_新能源汽车检修平台", "V1.2.1", "Expiry", "30");
        RegistryManager.SetRegistryKey("App_新能源汽车检修平台", "V1.2.2", "Expiry", "30");
        RegistryManager.SetRegistryKey("App_新能源汽车检修平台", "V1.2.3", "Expiry", "30");

    }

在这里插入图片描述





TechX —— 心探索、心进取!

每一次跌倒都是一次成长

每一次努力都是一次进步

END
感谢您阅读本篇博客!希望这篇内容对您有所帮助。如果您有任何问题或意见,或者想要了解更多关于本主题的信息,欢迎在评论区留言与我交流。我会非常乐意与大家讨论和分享更多有趣的内容。
如果您喜欢本博客,请点赞和分享给更多的朋友,让更多人受益。同时,您也可以关注我的博客,以便及时获取最新的更新和文章。
在未来的写作中,我将继续努力,分享更多有趣、实用的内容。再次感谢大家的支持和鼓励,期待与您在下一篇博客再见!
  • 24
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐沐森的故事

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值