Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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?
  • Leaders
Posted
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?
[sIGPIC]e[/sIGPIC]
Posted

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?

Posted

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;
}

  • Leaders
Posted

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]

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...