create file and save to it using memorystream

22,359

Solution 1

(Presuming you mean how to copy a file's content to a memory stream)

If you are using framework 4:

var memoryStream = new MemoryStream();

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

Solution 2

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();
Share:
22,359
Yustme
Author by

Yustme

Updated on December 11, 2020

Comments

  • Yustme
    Yustme over 3 years

    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.

    How can this be done?

  • Yustme
    Yustme over 13 years
    The data that is comming in, comes from a webbrouwser control. The html content of a website. That will be passed to a string and from there saved to a file. But before that, i do some string operations removing things that don't need to be saved like Flash en javascript.