Saving a Stream to disk??

A

Anibal Acosta

Hello I have a Stream (IO.Stream) with information insize, I want to save
this stream into a file in my disk "C:\myFile.exe"

How Can I do that without loosing information?

Thanks

AA
 
B

Branimir Giurov

Hi,

you can do one of two things:

1. read the stream into a byte array and write it to disk. Works if you know
the length of the stream. You should do that only when you deal with small
amount of data ('couse it stays in the memory for a while).
- or -
2.use a binary writer for primitive and low level operations - check out
BinaryReader Class in the MSDN documentation


Solution 1 Example:

using System;
using System.IO;

Stream s = File.OpenRead(@"c:\input.bin");
byte[] buff = new byte[s.Length];
s.Read(buff, 0, s.Length);

FileStream fs = File.Create("output.bin");
fs.Write(buff, 0, buff.Length);


Cheers,
Branimir
 
J

Jon Skeet [C# MVP]

Anibal Acosta said:
Hello I have a Stream (IO.Stream) with information insize, I want to save
this stream into a file in my disk "C:\myFile.exe"

How Can I do that without loosing information?

Well, creating a FileStream and then copying chunks across until you've
finished should work just fine. What have you tried, and in what way
isn't it working?
 

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

Top