Stream Writer

C

Charles A. Lackman

Hello,

I have created a MemoryStream that is holding Binary Data. How do I get the
Data out of the Memory Stream into a file on my hard drive. All the
examples I have seen for a MemoryStream write the contents to the console.

Thanks
Chuck
 
J

Jay B. Harlow [MVP - Outlook]

Chuck,
You can use MemoryStream.ToArray to get the MemoryStream as an array of
bytes.

You can then use a FileStream to write this array of bytes to a file.

Alternatively you could read the MemoryStream from the beginning writing the
data to a FileStream...

However if your goal is to write Binary Data to a file, why not simply open
a FileStream directly instead of using an intermediate MemoryStream?

Hope this helps
Jay
 
A

Arne Janning

Hi Charles!

I have created a MemoryStream that is holding Binary Data. How do I get
the Data out of the Memory Stream into a file on my hard drive. All the
examples I have seen for a MemoryStream write the contents to the console.

In addition to the possibilities Jay listed you can use the
MemoryStream.WriteTo()-Method:

Dim buffer As Byte() = New Byte() {1, 2, 3, 4, 5}
Dim ms As MemoryStream = New MemoryStream(buffer)
Dim st As FileStream = New FileStream( _
"C:\test.bin", FileMode.Create)
ms.WriteTo(st)
st.Close()
ms.Close()


Cheers

Arne Janning
 
C

Charles A. Lackman

Hello I am retrieve binary data from a web service and need to write it to
disk. It will be a setup file or replacement files I have made for
applications.

This is where I am lost

the memory stream is filled.

ms.Seek(0, SeekOrigin.Begin)
Dim MyWriter as StreamWriter
MyWriter = New StreamWriter("C:\Test.Setup")

ms.read
mywriter.write(ms)

ms.close
mywriter.close
This does not work, what am I missing,

Thanks
Chuck
 
J

Jon Skeet [C# MVP]

Charles A. Lackman said:
Hello I am retrieve binary data from a web service and need to write it to
disk. It will be a setup file or replacement files I have made for
applications.

This is where I am lost

the memory stream is filled.

ms.Seek(0, SeekOrigin.Begin)
Dim MyWriter as StreamWriter
MyWriter = New StreamWriter("C:\Test.Setup")

ms.read
mywriter.write(ms)

ms.close
mywriter.close
This does not work, what am I missing,

One point here, apart from what the others have said: if you've got
binary data, you shouldn't use StreamWriter. Writers and Readers are
for text data. Streams are for binary data. Just use FileStream.
 
Joined
Jan 16, 2008
Messages
1
Reaction score
0
File seems to be locked

Hi all,

I have the above method roughly in my code and all wors fine with creating and using a memory stream object. Once i have finished i am writting the results of the memorystream back to a file usign the following:-

Dim ms As System.IO.MemoryStream = [xmlmemorystreamobject]
ms.Position = 0

My.Computer.FileSystem.WriteAllBytes("C:\File.xml", ms.ToArray, False)

This works fine creates the file and contains all data correctly formatted.

However after this code i reference this file in order to place it in to a zip folder using the sharpziplib. Which at this stage does not put it in to the zip. This code works fine when tested with a seperate application even when running at the same time.

If i load the created file back to my memorystream object and re run the above code all is well.

I can only think there is a lock on the file still.

Any thoughts?

Cheers cg084
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top