Saving a Stream to disk??

  • Thread starter Thread starter Anibal Acosta
  • Start date Start date
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
 
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
 
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?
 
Back
Top