Is System.Net.WebClient.DownloadFile () reading the downloaded file in chunks?

Is System.Net.WebClient.DownloadFile () reading the downloaded file in chunks, or does it read the entire file into memory to flush it to disk at the end? Somebody knows? From my tests, it seems that the file is created with 0 bytes to the right, and then, after the download is complete, it will explode to full size. But for some reason it’s so hard to believe, because it would mean that 2 GB of RAM is busy when loading a 2 GB file ... it would be pointless.

+4
source share
3 answers

It writes everything received directly to FileStream. The DownloadBitsState.RetrieveBytes () method , a call to WriteStream.Write () if you want to take a look at Reflector or Reference Source. The size property is not updated in Explorer until the file is closed.

+2
source

Looking at the disassembled code (assembly System, Version = 4.0.0.0), I found the following: the DownloadFile () method calls the DownloadBits () method with asyncOp = null to load the file. When the download is complete, the method will return the entire downloaded file as byte []. Consequently, memory is lost, and you may encounter problems with large files.

+1
source

The response on the Internet is transmitted directly to the file; he does not hold it all in mind. The method blocks the stream until the entire file is loaded. You can use DownloadFileAsync() to download it asynchronously.

0
source

All Articles