A few options:
Read it all into a byte array first...
byte[] data = File.ReadAllBytes(file);
MemoryStream stream = new MemoryStream(data);
Or use .NET 4's CopyTo method
MemoryStream memoryStream = new MemoryStream();
using (Stream input = File.OpenRead(file))
{
input.CopyTo(memoryStream);
}
memoryStream.Position = 0;
Or do it manually
MemoryStream memoryStream = new MemoryStream();
using (Stream input = File.OpenRead(file))
{
byte[] buffer = new byte[32 * 1024]; // 32K buffer for example
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Write(buffer, 0, bytesRead);
}
}
memoryStream.Position = 0;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…