1 c# memorystream bytearray object binaryformatter
我试图将检索到的注册表值转换object
为byte[]
.它存储为REG_BINARY
.我试着用BinaryFormatter
用MemoryStream
.但是,它增加了我不想要的开销信息.当我通过执行该函数将字节数组转换为字符串时,我观察到了这一点Convert.ToBase64String(..)
.我正在执行这些功能,因为我正在测试在注册表中存储和检索加密密钥.
如果它是一个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
}
Run Code Online (Sandbox Code Playgroud)
试试这个.如果它已经是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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10414 次 |
最近记录: |