473,729 Members | 2,371 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 4657
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
9235
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
133819
by: Alexander Muylaert | last post by:
Hi What would be the easiest way to save a memorystream to a file? Kind regards Alexander
2
2132
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
4929
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
17844
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
6599
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
13792
by: ad | last post by:
I do know how to convert a FileStream to MemoryStream. Could somebody help me?
3
3606
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
5668
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
8921
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8763
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
9284
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...
1
9202
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8151
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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
4528
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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

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.