.net 如何将文件加载到内存流中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6213993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to load a file into memory stream
提问by Ram
I have a filename pointing to a text file, including its path, as a string. Now I'd like to load this .csvfile into memory stream. How should I do that?
我有一个文件名指向一个文本文件,包括它的路径,作为一个字符串。现在我想将此.csv文件加载到内存流中。我该怎么做?
For example, I have this:
例如,我有这个:
Dim filename as string="C:\Users\Desktop\abc.csv"
回答by Centro
Dim stream As New MemoryStream(File.ReadAllBytes(filename))
回答by SLaks
You don't need to load a file into a MemoryStream.
您不需要将文件加载到 MemoryStream 中。
You can simply call File.OpenReadto get a FileStreamcontaining the file.
您可以简单地调用File.OpenRead以获取FileStream包含该文件的文件。
If you really want the file to be in a MemoryStream, you can call CopyToto copy the FileStream to a MemoryStream.
如果您确实希望文件在 MemoryStream 中,则可以调用CopyTo将 FileStream 复制到 MemoryStream。
回答by Zapnologica
You can copy it to a file stream like so:
您可以将其复制到文件流,如下所示:
string fullPath = Path.Combine(filePath, fileName);
FileStream fileStream = new FileStream(fullPath, FileMode.Open);
Image image = Image.FromStream(fileStream);
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Jpeg);
//Close File Stream
fileStream.Close();

