Create a file and save it with memystream

How to create a file and write it using a memory stream? I need to use memystream so that other threads do not try to access the file.

The data I'm trying to save in a file is html.

How can I do that?

+3
source share
2 answers

(Suppose you mean how to copy the contents of a file into a memory stream)

If you are using framework 4:

     MemoryStream memoryStream = new MemoryStream();
     using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
     {
       fileStream.CopyTo(memoryStream);
     }
+5
source

Here is the code to create the 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();
+2
source

Source: https://habr.com/ru/post/1768383/


All Articles