简体   繁体   English

创建文件并使用内存流保存到它

[英]create file and save to it using memorystream

How can i create a file and write to it using the memory stream?如何使用内存流创建文件并写入文件? I need to use the memorystream to prevent other threads from trying to access the file.我需要使用内存流来防止其他线程尝试访问该文件。

The data i'm trying to save to a file is html.我试图保存到文件的数据是 html。

How can this be done?如何做到这一点?

(Presuming you mean how to copy a file's content to a memory stream) (假设您的意思是如何将文件的内容复制到内存流)

If you are using framework 4:如果您使用的是框架 4:

var memoryStream = new MemoryStream();

using var fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
fileStream.CopyTo(memoryStream);

Here are code to create file这是创建文件的代码

byte[] data = System.Text.Encoding.ASCII.GetBytes("This is a sample string");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ms.Write(data, 0, data.Length);
ms.Close();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM