我正在尝试将检索到的注册表值从 转换object
为byte[]
. 它存储为REG_BINARY
. 我尝试使用BinaryFormatter
with MemoryStream
。但是,它添加了我不想要的开销信息。当我通过执行函数将字节数组转换为字符串时,我观察到了这一点Convert.ToBase64String(..)
。我正在执行这些功能,因为我正在测试注册表中加密密钥的存储和检索。
user195488
问问题
11184 次
3 回答
8
如果它是一个 REG_BINARY 那么当你检索它时它应该已经是一个字节数组......你不能把它转换成byte[]
?
或者,如果您尚未在代码中验证它是 REG_BINARY,您可能需要使用:
byte[] binaryData = value as byte[];
if (binaryData == null)
{
// Handle case where value wasn't found, or wasn't binary data
}
else
{
// Use binaryData here
}
于 2011-01-13T20:24:20.303 回答
5
试试这个。如果它已经是一个 REG_BINARY,你需要做的就是转换它:
static byte[] GetFoo()
{
var obj = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\\Software", "foo", null);
//TODO: Write a better exception for when it isn't found
if (obj == null) throw new Exception();
var bytearray = obj as byte[];
//TODO: Write a better exception for when its found but not a REG_BINARY
if (bytearray == null) throw new Exception();
return bytearray;
}
于 2011-01-13T20:28:24.340 回答
0
如果您使用 Convert.ToBase64String 对其进行了转换,您应该能够以类似方式将其取出。
string regValueAsString = (string)regValueAsObj;
byte[] buf = Convert.FromBase64String(regValueAsString);
于 2011-01-13T20:24:00.793 回答