[英]How can I create a file from a byte array and send it to the stream as a file without creating this file on the disk?
是否可以从字节数组创建文件并将其作为文件直接发送到流,而无需在磁盘上创建此文件?
There is a MemoryStream class that takes a byteArray as argument.有一个将 byteArray 作为参数的MemoryStream类。 You don't need to write a file.
你不需要写文件。
Stream stream = new MemoryStream(byteArray);
Alternatively, you can Write
to an existing MemoryStream
:或者,您可以
Write
现有的MemoryStream
:
memStream.Write(byteArray, 0 , byteArray.Length);
Your question is not very clear.你的问题不是很清楚。 You are asking to "create a file" and yet you are saying "without creating this file on disk".
您要求“创建文件”,但您说的是“不在磁盘上创建此文件”。 However it is possible to either "create a file from a byte array" :
但是,可以“从字节数组创建文件”:
File.WriteAllBytes( path, bytes );
where path is the filename to create and bytes is the byte[] to write - this is just one of those many ways.其中 path 是要创建的文件名, bytes 是要写入的 byte[] - 这只是众多方式中的一种。
Or "send to a stream without creating file on disk".或者“发送到流而不在磁盘上创建文件”。 ie: Write to MemoryStream:
即:写入 MemoryStream:
var ms = new MemoryStream( bytes );
I am afraid your question needs to be more specific on what you are trying to do.恐怕您的问题需要更具体地说明您要做什么。
Edit: Crypto sample:编辑:加密示例:
private static byte[] Crypt
(byte[] data, byte[] key, byte[] iv, ICryptoTransform cryptor)
{
MemoryStream m = new MemoryStream();
using( Stream c = new CryptoStream(m, cryptor, CryptoStreamMode.Write ))
{
c.Write(data, 0, data.Length);
}
return m.ToArray();
}
public static byte[] Encrpyt(byte[] data, byte[] key, byte[] iv)
{
using( Aes algorithm = Aes.Create())
using( ICryptoTransform encryptor = algorithm.CreateEncryptor(key,iv))
{
return Crypt( data, key, iv, encryptor );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.