[英]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: 这里是一些开始的想法:
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.