music7611 Posted April 4, 2005 Posted April 4, 2005 Hi all. I have a two streams in C#. One is one that comes from a file on the internet, the other is a file on the computer. I need a fairly quick way to copy the content on the file on the net to the file on the computer. What is the best way to do this? Quote
Leaders snarfblam Posted April 5, 2005 Leaders Posted April 5, 2005 Can't you use a stream reader (IO.StreamReader) to read the whole file at once and then a stream writer (IO.StreamWriter) to write the whole file at once? Quote [sIGPIC]e[/sIGPIC]
music7611 Posted April 5, 2005 Author Posted April 5, 2005 I thought of that, but won't that mean if you want to download a big file, you have to keep it all in the ram? Also, for binary files, StreamReader messes up the files. Is there a BinaryReader/Writer equivilent? Quote
HJB417 Posted April 5, 2005 Posted April 5, 2005 When I encountered a similar situation in the past, I created a method to 'copy the streams'. private static long CopyData(Stream input, Stream output) { int amtRead = 1; long amtCopied = 0; byte[] buffer = new byte[8192*8]; while(amtRead > 0) { amtRead = input.Read(buffer, 0, buffer.Length); amtCopied+=amtRead; output.Write(buffer, 0, amtRead); } return amtCopied; } Quote
Leaders snarfblam Posted April 5, 2005 Leaders Posted April 5, 2005 well... couldn't you just declare a byte array, download blocks at a time and write them? Dim Data() As Byte = New Byte(InsertSizeHere) Do Length = WebStream.Read(Data, 0, Data.Length) FileStream.Write(Data, 0, Length) Loop While WebStream.Length < WebStream.Position - 1 Didn't test this, but you can get the jist of it. And if performance becomes an issue you could look into using asynchronious methods (i.e. use BeginRead(), EndRead(), BeginWrite(), etc.) [Edit]I guess someone beat me to giving you the same answer, and I know you were asking about C# (not that it would be hard to translate). Oh well.[/Edit] Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.