简体   繁体   English

在C#中使用内存中文件的最佳方法是什么?

[英]What is the best way to work with files in memory in C#?

I am building an ASP.NET web application that creates PowerPoint presentations on the fly. 我正在构建一个ASP.NET Web应用程序,该应用程序可以动态创建PowerPoint演示文稿。 I have the basics working but it creates actual physical files on the hard disk. 我有基本的工作原理,但是它会在硬盘上创建实际的物理文件。 That doesn't seem like a good idea for a large multi-user web application. 对于大型的多用户Web应用程序来说,这似乎不是一个好主意。 It seems like it would be better if the application created the presentations in memory and then streamed them back to the user. 如果应用程序在内存中创建演示文稿,然后将其流回给用户,这似乎会更好。 Instead of manipulating files should I be working with the MemoryStream class? 我应该不使用文件来处理MemoryStream类吗? I am not exactly sure I understand the difference between working with Files and working with Streams. 我不确定我是否了解使用文件和使用流之间的区别。 Are they sort of interchangeable? 它们是可互换的吗? Can anyone point me to a good resource for doing file type operations in memory instead of on disk? 谁能指出我一个很好的资源来在内存中而不是磁盘上执行文件类型的操作? I hope I have described this well enough. 我希望我已经描述得足够好了。

Corey 科里

You are trying to make decision that you think impacts performance of your application based on "doesn't seem like a good idea" measurement, which is barely scientific. 您正在尝试基于“似乎不是一个好主意”度量标准来做出您认为会影响应用程序性能的决策,这几乎是不科学的。 It would be better to implement both and compare, but first you should list your concerns about either implementations. 最好将两者实现并进行比较,但是首先您应该列出对两种实现的关注。

Here are some ideas to start: 这里是一些开始的想法:

  • there are really not much difference between temporary files and in-memory streams. 临时文件和内存流之间确实没有太大区别。 Both would have content in physical memory if they are small enough, both will hit the disk if there is memory pressure. 如果两者都足够小,则两者都将在物理内存中包含内容;如果存在内存压力,则两者都将撞击磁盘。 Consider using temporary Delete on close files for your files if cleaning files up is the main concern. 如果主要需要清理文件,请考虑对关闭文件使用临时删除
  • OS already doing very good job for managing large files with caching, one would need to make sure pure in-memory solution at least matches it. 操作系统已经非常适合使用缓存管理大型文件,因此需要确保纯内存中的解决方案至少与之匹配。
  • MemoryStream is not the best implementation for reasonably sized streams due its "all data is in single byte array" contract (see my answer at https://stackoverflow.com/a/10424137/477420 ). MemoryStream不是合理大小的流的最佳实现,因为它的“所有数据都在单字节数组中”协定(请参阅我在https://stackoverflow.com/a/10424137/477420上的答复)。
  • Managing multiple large in-memory streams (ie for multiple users) is fun for x86 platform, less of a concern for x64 ones. 对于x86平台来说,管理多个大型内存流(例如,针对多个用户)很有趣,而对于x64平台则不那么关心。
  • Some API simply don't provide a way working with Stream-based classes and require physical file. 某些API根本不提供使用基于Stream的类的方式,并且需要物理文件。

Files and streams are similar, yes. 文件和流相似,是的。 Both essentially stream a byte array...one from memory, one from the hard drive. 两者本质上都是流式传输byte数组……一个来自内存,一个来自硬盘驱动器。 If the API you are using allows you to generate a stream, then you can easily do that and serve it out to the user using the Response object. 如果您使用的API允许您生成流,那么您可以轻松地做到这一点,并使用Response对象将其提供给用户。

The following code will take a PowerPoint memory object (you'll need to modify it for your own API, but you can get the general idea), save it to a MemoryStream , then set the proper headers and write the stream to the Response (which will then let the user save the file to their local computer): 以下代码将使用PowerPoint内存对象(您需要针对自己的API对其进行修改,但您可以得到一般的想法),将其保存到MemoryStream ,然后设置适当的标头并将流写入Response(然后,它将使用户将文件保存到本地计算机):

SaveFormat format = SaveFormat.PowerPoint2007;
Slideshow show = PowerPointWriter.Generate(report, format);
MemoryStream ms = new MemoryStream();
show.Save(ms, format);

Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-powerpoint";
Response.AddHeader("Content-Disposition", "attachment; filename=\"Slideshow.ppt\"");
Response.BinaryWrite(ms.ToArray());
Response.End();

Yes, I would recommend the MemoryStream. 是的,我建议使用MemoryStream。 Typically any time you access a file, you are doing so with a stream. 通常,每次访问文件时,都使用流。 There are many kinds of streams (eg network streams, file streams, and memory streams) and they all implement the same basic interface. 流有很多种类(例如网络流,文件流和内存流),它们都实现相同的基本接口。 If you are already creating the file in a file stream, instead of something like a string or byte array, then it should require very little coding changes to switch to a MemoryStream. 如果您已经在文件流中创建文件,而不是在字符串或字节数组之类的文件中创建文件,则只需很少的代码更改即可切换到MemoryStream。

Basically, a steam is simply a way of working with large amounts of data where you don't have to, or can't, load all the data at into memory at once. 基本上,Steam只是处理大量数据的一种方式,您不必或不必立即将所有数据立即加载到内存中。 So, rather than reading or writing the entire set of data into a giant array or something, you open a stream which gives you the equivalent of a cursor. 因此,您不必打开整个数据流,而是将其等同于游标,而不是将整个数据集读取或写入一个巨型数组或其他东西。 You can move your current position to any spot in the stream and read or write to it from that point. 您可以将当前位置移动到流中的任何位置,然后从该位置读取或写入该位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中使用多线程处理多个文件的最佳方法是什么? - What is the best way to work with multiple files in multithread in C#? 在c#中使用线程工作的最佳方法是什么? - What is the best way to thread work in c#? 衡量C#函数的内存使用率的最佳方法是什么? - What is the best way to measure the memory usage of a C# function? 在C#中使用PHP和MySQL的最佳方法是什么 - What is the best way to work with PHP and MySQL in C# 在C#中转换Fortran头文件.INC的最佳方法是什么 - What is the best way to convert Fortran Header Files .INC in c# 在C#中的项目中使用图像文件的最佳方法是什么 - What is best way to use image files in my project in C# 在C#中生成KML文件的最佳方法是什么? - What is the best way to generate KML files in C#? 在c#中对250万条内存中的记录进行排序的最佳方法是什么? - What's the best way to sort about 2.5 million records in memory in c#? 用C#对象查找进程的内存分配的最佳方法是什么 - What is the best way to find a process's memory allocations in terms of C# objects 在c#中实现队列的最佳方法是什么(System.Collection.Queue有内存限制) - What is best way to implement a queue in c#(System.Collection.Queue has memory limitation)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM