473,698 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4655
On Sep 17, 4:40*pm, paradigmshift <chrismsm...@gm ail.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*********@gm ail.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*********@gm ail.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.co m>
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
9223
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 printer port. I found the SendFileToPrinter example below in a knowledge base at MS site It works, but I need to replace the filestream with a memorystream cause there's no need to write files and then load them again just to print. So I rewrote...
8
133817
by: Alexander Muylaert | last post by:
Hi What would be the easiest way to save a memorystream to a file? Kind regards Alexander
2
2129
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? I have a sub that calls a function: Dim bByte() As Byte bByte = ws.SQLGetUserDemographics()
1
4926
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 the StreamReader(which can read in big5 encoding text files well) and besides, I have tried the BufferedStream to do the same thing. It does work fine except that It can't interpret the Chinese big5 words well(which becomes messy code around)....
10
17840
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." Source="System.Xml" The code:
1
6594
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
13778
by: ad | last post by:
I do know how to convert a FileStream to MemoryStream. Could somebody help me?
3
3604
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 FileStream but it locked the xml file so I can't access it anymore until I close the filestream, so my question is how to read from the file to the memorystream directly or read to the filestream first and the copy it to the memorystream? Thanks
4
5660
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) On each FileSystemItem Element, make two child nodes, one with the file name, one with the file size. ie. <filesystemitems> <filesystemitem>
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6521
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.