Downloading a file from a website

  • Thread starter Thread starter Chris Morrison
  • Start date Start date
C

Chris Morrison

Hi all,

Can anyone tell me which .NET class I need (or provide a code sample) to
download file from a website/url (Not FTP) and save it to my hard drive.

Kind regards,



Chris
 
What do you want to do? You don't need .NET for that. <A
HREF="file.ext">Click to download</A>


"Chris Morrison"
 
use the following code:

url - the url of the file to be downloaded.
// Create a request to the URL.

WebRequest request = WebRequest.Create(url);

// Get response from the request made, as a stream

WebResponse response = request.GetResponse();

Stream stream = response.GetResponseStream();

StreamReader reader = new StreamReader(stream);

// read the content of the stream as a string.

string content = reader.ReadToEnd();

// save the same in a temp file.

"Chris Morrison"
 
Back
Top