How do I download a large file (via HTTP) in .NET?

前端 未结 6 1145
余生分开走
余生分开走 2021-02-01 18:03

I need to download a large file (2 GB) over HTTP in a C# console application. Problem is, after about 1.2 GB, the application runs out of memory.

Here

6条回答
  •  面向向阳花
    2021-02-01 18:15

    You need to get the response stream and then read in blocks, writing each block to a file to allow memory to be reused.

    As you have written it, the whole response, all 2GB, needs to be in memory. Even on a 64bit system that will hit the 2GB limit for a single .NET object.


    Update: easier option. Get WebClient to do the work for you: with its DownloadFile method which will put the data directly into a file.

提交回复
热议问题