user397206
user397206

Reputation: 29

Download a file from the internet?

How can I easily download a file from internet in C#?

Thank you very much.

Upvotes: 0

Views: 896

Answers (2)

Have a look at the BCL class System.Net.WebRequest. Here's a brief example:

using System.Net;
...

var request = WebRequest.Create(new Uri("http://..."));
var response = request.GetResponse();
var responseStream = response.GetResponseStream();
//  ^^^^^^^^^^^^^^
//  read web resource content through this stream object!
...

Take note:

  • Some of these objects are IDisposable, so in real code you might want to wrap them in using blocks.

  • The above code example does not do any error checking. You might want to add appropriate code for this, also.

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564323

WebClient.DownloadFIle is one simple way.

Upvotes: 8

Related Questions