就像在主题中一样:我想将文件(从流)中的数据读取到内存(内存流)中以提高我的应用程序速度.怎么做?
Like in the topic: I want to read data from a file (from stream) into memory (memorystream) to improve my app speed. How to do it?
几个选项:
先将其全部读入一个字节数组...
byte[] data = File.ReadAllBytes(file);
MemoryStream stream = new MemoryStream(data);
或者使用 .NET 4 的 CopyTo 方法
MemoryStream memoryStream = new MemoryStream();
using (Stream input = File.OpenRead(file))
{
input.CopyTo(memoryStream);
}
memoryStream.Position = 0;
或者手动操作
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;
这篇关于如何将整个流加载到 MemoryStream 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
LINQ to SQL:如何在不检索整个实体的情况下更新唯LINQ to SQL: how to update the only field without retrieving whole entity(LINQ to SQL:如何在不检索整个实体的情况下更新唯一字段)
string1 >= string2 未在 Linq to SQL 中实现,有什string1 gt;= string2 not implemented in Linq to SQL, any workaround?(string1 gt;= string2 未在 Linq to SQL 中实现,有什么解决方法吗?)
如何在条件下使用 RemoveAll 删除列表中的多个项目How to Remove multiple items in List using RemoveAll on condition?(如何在条件下使用 RemoveAll 删除列表中的多个项目?)
转换表达式树Convert Expression trees(转换表达式树)
当 IDENTITY_INSERT 设置为 OFF 时,无法为表“ClientCannot insert explicit value for identity column in table #39;ClientDetails#39; when IDENTITY_INSERT is set to OFF(当 IDENTITY_INSERT 设置为 OFF 时,
Linq 独特的 &最大限度Linq distinct amp; max(Linq 独特的 amp;最大限度)