gpt4 book ai didi

c# - 如何从注册表读取二进制数据到字节数组

转载 作者:可可西里 更新时间:2023-11-01 12:43:44 27 4
gpt4 key购买 nike

我使用以下代码将字节数组保存到注册表

Byte[] value = new byte[16]{
0x4a,0x03,0x00,0x00,
0x45,0x02,0x00,0x00,
0xb7,0x00,0x00,0x00,
0x9d,0x00,0x00,0x00
};

RegistryKey key = Registry.CurrentUser.CreateSubKey(KeyName);
key.SetValue(@"Software\Software\Key", value, RegistryValueKind.Binary);

这是使用上面的代码创建的 key :

[HKEY_CURRENT_USER\Software\Software\Key]  
"LOC"=hex:4a,03,00,00,45,02,00,00,b7,00,00,00,9d,00,00,00

现在我想将相同的数据读回字节数组格式。以下代码可以读取相同的数据,但输出的是对象类型。

RegistryKey key = Registry.CurrentUser.OpenSubKey(KeyName);
object obj = key.GetValue(@"Software\Software\Key", value);

此处转换为 byte[] 无效。我知道我可以使用序列化程序或流来完成此任务。我想知道是否有更简单的方法将数据读回 byte[] 类型(两行代码)?

请备注this question在 C++ 中

最佳答案

要将字节数组写入注册表,请使用以下代码

Byte[] value = new byte[]{
0x4a,0x03,0x00,0x00,
0x45,0x02,0x00,0x00,
0xb7,0x00,0x00,0x00,
0x9d,0x00,0x00,0x00
};

RegistryKey key = Registry.CurrentUser.CreateSubKey(KeyName);
key.SetValue(@"Software\AppName\Key", value, RegistryValueKind.Binary);

要将数据从注册表中检索回 Byte[] 格式,请使用以下命令:

RegistryKey key = Registry.CurrentUser.OpenSubKey(KeyName);
byte[] Data = (byte[]) key.GetValue(@"Software\AppName\Key", value);

注意:CurrentUser 是您 key 位置的根名称,指向 HKEY_CURRENT_USER

关于c# - 如何从注册表读取二进制数据到字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14374967/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com