473,544 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MemoryStream.Sa veToFile

Hi

What would be the easiest way to save a memorystream to a file?

Kind regards

Alexander

Nov 16 '05 #1
8 133803
Convert it to binary filestream or just write byte ofter byte.
If you need an example there are about 20 in MSDN

--
Tamir Khason
Who am I at
http://www.khason.biz/
"Alexander Muylaert" <Re********@pan dora.be> wrote in message
news:u6******** ******@TK2MSFTN GP11.phx.gbl...
Hi

What would be the easiest way to save a memorystream to a file?

Kind regards

Alexander

Nov 16 '05 #2
Alexander Muylaert <Re********@pan dora.be> wrote:
What would be the easiest way to save a memorystream to a file?


Well, the *easiest* way would be to open a file stream and then use:

byte[] data = memoryStream.To Array();
fileStream.Writ e(data, 0, data.Length);

That's relatively inefficient though, as it involves copying the
buffer. It's fine for small streams, but for huge amounts of data you
should consider using:

fileStream.Writ e(memoryStream. GetBuffer(), 0, memoryStream.Po sition);

(assuming the memory stream is already positioned at the end).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
// ms is your memoryStream
FileStream fs = File.OpenWrite( "blah.dat") ;
/* could replace with GetBuffer() if you don't mind the padding, or you
could set Capacity of ms to Position to crop the padding out of the
buffer.*/
fs.Write(ms.ToA rray());

fs.Close();
--
John Wood
EMail: first name, dot, last name, at priorganize.com
"Alexander Muylaert" <Re********@pan dora.be> wrote in message
news:u6******** ******@TK2MSFTN GP11.phx.gbl...
Hi

What would be the easiest way to save a memorystream to a file?

Kind regards

Alexander

Nov 16 '05 #4
Tamir Khason <ta**********@t con-NOSPAM.co.il> wrote:
Convert it to binary filestream or just write byte ofter byte.
If you need an example there are about 20 in MSDN


Writing individual bytes is likely to be very much slower than writing
the whole buffer in one go.

Not sure what you meant by "convert it to binary filestream" to be
honest...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Hi guys

Are you seriously telling me that there is no way I can write the internall
array fast to a file?
No optimized routine that writes a memorystream in blocks to a file?

In Delphi you have TMemorystream.S aveToFile.
This actually writes the memorystream to file, optimized for disk
performance. (blocks of 8 k etc..)

Kind regards

Alexander
Nov 16 '05 #6
I don't understand what the problem is.

The code:

FileStream fs = File.OpenWrite( "blah.dat") ;
fs.Write(ms.Get Buffer(), 0, ms.Position);
fs.Close();

Is pretty damn fast, and short. Any write-based optimizations will be taken
care of by the FileStream implementation and shouldn't be something you have
to worry about.

I think, from a design point of view, it would be wrong to have a SaveToFile
function in the MemoryStream object. The purpose of the MemoryStream is just
that, to provide a stream interface onto a block of memory. The FileStream
writes to files, and so it makes sense to use a FileStream to dump out the
internal buffer of the MemoryStream (which is what GetBuffer() provides) to
a file.

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"Alexander Muylaert" <Re********@pan dora.be> wrote in message
news:eV******** ******@TK2MSFTN GP11.phx.gbl...
Hi guys

Are you seriously telling me that there is no way I can write the internall array fast to a file?
No optimized routine that writes a memorystream in blocks to a file?

In Delphi you have TMemorystream.S aveToFile.
This actually writes the memorystream to file, optimized for disk
performance. (blocks of 8 k etc..)

Kind regards

Alexander

Nov 16 '05 #7
Okay

I Found the WriteTo.

Thanks a lot.

Kind regards

Alexander


Nov 16 '05 #8
Alexander,
In addition to the other suggestions.

I would use MemoryStream.Wr iteTo, giving it a FileStream.

http://msdn.microsoft.com/library/de...iteToTopic.asp

Hope this helps
Jay

"Alexander Muylaert" <Re********@pan dora.be> wrote in message
news:u6******** ******@TK2MSFTN GP11.phx.gbl...
Hi

What would be the easiest way to save a memorystream to a file?

Kind regards

Alexander

Nov 16 '05 #9

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

Similar topics

1
11128
by: xmlguy | last post by:
PREVIOUS BACKGROUND POST: I am trying to reuse a Memory Stream for loading and transforming the xml it contains Basically I have defined following interfaces: Class Render {
0
382
by: gerry | last post by:
I have an async OnStore event written in c# and registered in exchange 2003. Everything seems to be working fine other than the SaveToFile(). If there is an error in the iMessage.DataSource.Open ( ie. the message has moved ) the expected exception is raised and caught. However when the strm.SaveToFile() is executed the process just seems to...
3
10942
by: Nicolas | last post by:
Hi Everybody, I'm working with the MemoryStream and I'm having problems with the ReadBytes method. When I use it, this method never returns bytes!!!! MemoryStream sw = new MemoryStream(); sw.Write(System.Text.UnicodeEncoding.Unicode.GetBytes("Testing"),0,6); System.Text.StringBuilder sb = new System.Text.StringBuilder(); byte buffer = new...
2
2118
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()
13
2832
by: Don | last post by:
When I run the following code, the MemoryStream's Position is always set to 762 instead of 0, which is what I would expect: Dim bmp As Image Dim ms As MemoryStream bmp = New System.Drawing.Bitmap("C:\2068.bmp") ms = New MemoryStream bmp.Save(ms, bmp.RawFormat)
10
17820
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:
4
8412
by: Heron | last post by:
Hi, Could someone explain me why the following code doesn't work? The memorystream always remains with length 0. MemoryStream input = new MemoryStream();
2
2983
by: asenthil | last post by:
Hai to all, i want to save the contents of an existing file into a new file.... For example if i'm having word document file namely.... 1.doc... i want to save the contents into a new file namely 2.doc... i think the first step is..
3
5539
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, I am using dotnet remoting with a binarry formatter. I have a property that returns a memorystream that has had a file loaded into it. When I try to access this property though I get an error regarding "the proxy has no channel sink.......or no suitable Client channel to talk to the server."
0
7413
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...
0
7356
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...
0
7597
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. ...
0
7752
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...
1
7358
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...
0
7697
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...
1
5286
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...
0
3397
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...
0
650
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...

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.