473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Filestream to Memorystream

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,
Sep 17 '08 #1
3 4637
On Sep 17, 4:40*pm, paradigmshift <chrismsm...@gmail.comwrote:
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
Sep 17 '08 #2
On Wed, 17 Sep 2008 13:40:55 -0700, paradigmshift <ch*********@gmail.com>
wrote:
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.
Just read it from the FileStream and write it to the MemoryStream. As an
optimization, start by setting the Capacity property of the MemoryStream
to the length of the original FileStream. That will allow you to avoid
intermediate reallocations as the MemoryStream is written to.

That said, it's not really clear what advantage you intend to have by
copying all of the data to a MemoryStream. The system will still wind up
caching the data, but through a different mechanism (virtual memory rather
than via the file system). Copying all of the data from a file to a
memory block simply increases the memory pressure for the overall system,
which may in fact result in counter-productive behavior (e.g. data being
flushed from memory and needing to be reloaded later).

I encourage you to do some real-world performance comparisons if you
really want to try this. I suspect that you will find that the
performance improvement is minimal, assuming you don't actually make
things worse.

Pete
Sep 17 '08 #3
paradigmshift <ch*********@gmail.comwrote:
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!

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 17 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kai Bohli | last post by:
Hi all ! This message has been posted on .net.general, but I reposts it here due to lack of replies. This is my first day with Visual Studio and C#, and I'm trying to send "raw data" to the...
8
by: Alexander Muylaert | last post by:
Hi What would be the easiest way to save a memorystream to a file? Kind regards Alexander
2
by: SQLScott | last post by:
For some reason I am drawing a blank on this, so I would be extremely greatful if someone could help me out. I am trying to get a MemoryStream out of a Byte array. How do I get it back out? ...
1
by: foreman | last post by:
Hi there, Hello everybody. I am a newbie to dot net framework class lib. I am confused about those classes such as all of the stream classes and those XXXReader XXXWriter. In fact, I have tried...
10
by: Asaf | last post by:
Hi, I am trying to Compress & Decompress a DataSet. When running this code I am receiving the error: System.Xml.XmlException was unhandled Message="Root element is missing."...
1
by: pgmfan | last post by:
What is the difference among filestream, memoryStream and byte stream, how do i use each properly. i confused by them appreciate for any advice ,please help
1
by: ad | last post by:
I do know how to convert a FileStream to MemoryStream. Could somebody help me?
3
by: Yehia A.Salam | last post by:
Hello, I am trying to read from an xml file and put it to a memory stream so I can read it multiple times from the beginning using XmlTextReader without accessing the harddisk , I tried using...
4
by: | last post by:
Hi all, I want to create a method that does the following: 1) Programmatically instantiate a new XmlDataSource control 2) For each file in a named directory, make a "FileSystemItem" element 3)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.