5

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?

7
  • So why not save the data directly to memory stream? Commented Oct 7, 2010 at 7:54
  • What do ya mean? I want the data to be saved in a file. Commented Oct 7, 2010 at 7:56
  • Will the file be potentially modified after its creation? Commented Oct 7, 2010 at 8:02
  • No it will be potentially read. Commented Oct 7, 2010 at 8:07
  • Then it's safe to read it from multiple threads. Commented Oct 7, 2010 at 8:27

2 Answers 2

10

(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);
Sign up to request clarification or add additional context in comments.

1 Comment

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.
3

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.