tags:

views:

129

answers:

2

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

Thank you very much.

+6  A: 

WebClient.DownloadFIle is one simple way.

Reed Copsey
+2  A: 

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.

stakx