Filestream to Memorystream

P

paradigmshift

I am looking at how to move data from a file to a memory stream. This
data will be needed many times over and over so i don't want to have
to rely on the system to keep the information cashed for access. Kind
of an easy question for pros but thats why I figured I would ask here.

Thanks,
 
I

Ignacio Machin ( .NET/ C# MVP )

I am looking at how to move data from a file to a memory stream. This
data will be needed many times over and over so i don't want to have
to rely on the system to keep the information cashed for access. Kind
of an easy question for pros but thats why I figured I would ask here.

Thanks,

Just cpy the FileStream to the MemoryStream.
Unfortunately FileStream does not implement a WriteTo a la
MemoryStream , so you need a temp buffer
 
J

Jon Skeet [C# MVP]

paradigmshift said:
I am looking at how to move data from a file to a memory stream. This
data will be needed many times over and over so i don't want to have
to rely on the system to keep the information cashed for access. Kind
of an easy question for pros but thats why I figured I would ask here.

Create a buffer (byte array) and the memory stream. Repeatedly read
from the file stream into the buffer, and then write the contents (but
only as much as you read) into the memory stream. Keep going until
reading from the file stream yields no data.

See http://pobox.com/~skeet/csharp/readbinary.html for the reading loop
- the writing to the memory stream is the easy bit!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top