简体   繁体   English

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

[英]How can I read binary data from registry to byte array

I saved a byte array to registry using following code 我使用以下代码将一个字节数组保存到注册表中

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);

Here is the key created using above code: 这是使用上面代码创建的密钥:

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

Now I want to read the same data back to byte array format. 现在我想将相同的数据读回字节数组格式。 Following code can read the same data but the output is of type object. 以下代码可以读取相同的数据,但输出的类型为object。

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

Here casting to byte[] does not work. 这里转换为byte []不起作用。 I know I can use serializer or streams to achieve this task. 我知道我可以使用序列化程序或流来完成这项任务。 I would like to know if there is an easier way to read data back to byte[] type (A two liner code)? 我想知道是否有更简单的方法将数据读回byte []类型(双线代码)?

Please note this question is in C++ 请注意这个问题是用C ++编写的

To write a byte array to registry use following code 要将一个字节数组写入注册表,请使用以下代码

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);

To Retrieve the data back from registry into Byte[] format use following: 要将数据从注册表检索回Byte []格式,请使用以下命令:

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

Note: CurrentUser is name of the root for your Key location and points to HKEY_CURRENT_USER 注意: CurrentUser是Key位置的根名称,并指向HKEY_CURRENT_USER

I test in VB.NET: 我在VB.NET中测试:

Dim obj As Object = key.GetValue("Software\Software\Key", value__1)`
Dim v As [Byte]() = CType(obj, Byte())`

and it works 它的工作原理

so in C# should be: 所以在C#应该是:

Byte[] v = Convert.ToByte(obj);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C#,如何将二进制数据的字节数组转换为对数据建模的自定义类型对象? - With C#, how can I convert a byte array of binary data into a custom-typed object that models the data? 如何在DataTable中使用SqlBulkCopy和二进制数据(byte [])? - How can I use SqlBulkCopy with binary data (byte[]) in a DataTable? 如何“按原样”将二进制数据写入注册表(即:我只有可见的二进制数据作为字符串来自regedit) - How to write Binary Data “as is” to Registry (ie: i have visible binary data as string from regedit only) C#如何从注册表中读取MultiString - C# How can I read a MultiString from the registry 字节数组到我可以在 PHP 中读取的内容 - byte array to something I can read in PHP 我可以阅读注册表权限吗? 如果是这样,怎么办? - Can I read registry permissions? If so, how? 如何从c#中读取注册表数据 - How to read registry data from c# 如何在没有 I/O 的情况下对字节数组的数据保存加载使用 dlib 人脸检测? - How can I use dlib face detection without I/O on data saveload from byte array? 如何从byte []数组中获取数组的一部分 - How can I get part of the array from a byte[] array 如何从字节数组转换为通用数组? - How can I convert from a Byte Array to Generic Array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM