簡體   English   中英

如何從注冊表到字節數組讀取二進制數據

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

我使用以下代碼將一個字節數組保存到注冊表中

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

這是使用上面代碼創建的密鑰:

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

現在我想將相同的數據讀回字節數組格式。 以下代碼可以讀取相同的數據,但輸出的類型為object。

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

這里轉換為byte []不起作用。 我知道我可以使用序列化程序或流來完成這項任務。 我想知道是否有更簡單的方法將數據讀回byte []類型(雙線代碼)?

請注意這個問題是用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

我在VB.NET中測試:

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

它的工作原理

所以在C#應該是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM