// Optional: Specify proxy server settings //webClient.Proxy = new WebProxy("http://proxyserver:80/", true); //webClient.Proxy.Credentials = new NetworkCredential("user", "password");
// Optional: Add "User-Agent" header //webClient.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)";
// Download the file and write it to disk using (Stream webStream = webClient.OpenRead(url)) using (FileStream fileStream = new FileStream(outputFile, FileMode.Create))
{ var buffer = newbyte[32768]; int bytesRead;
Int64 bytesReadComplete = 0; // Use Int64 for files larger than 2 gb
// Get the size of the file to download
Int64 bytesTotal = Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]);
// Start a new StartWatch for measuring download time
Stopwatch sw = Stopwatch.StartNew();
// Download file in chunks while ((bytesRead = webStream.Read(buffer, 0, buffer.Length)) > 0)
{
bytesReadComplete += bytesRead;
fileStream.Write(buffer, 0, bytesRead);