SIGN UP MEMBER LOGIN:    
ARTICLE

How to Download File and Showing its Progress in Progress Bar

Posted by Kirtan Patel Articles | Windows Forms C# February 15, 2010
In this article I will explain about how to download file and showing its progress in progress bar.
Reader Level:
 

Introduction

Some time when you download a file from the internet using webclient we need to show progress bar to the user indicating how much task is accomplished. This can be very difficult and cumbersome when using Webclient.DownloadFile() method because it freeze the interface and we need to do extra coding by thread to show progress bar.

Here I am going to show you how to do that task very simply.

Technologies : .NET 2.0/3.5
Language : C#
Prerequisite :

  1. .NET Framework 3.5/2.0
  2. Visual Studio 2008/2005

Implementation

Lets get started, fist of all let setup the GUI like below:

Take a label, Text Box, Progress bar and a Button.

image1.gif

Now in code first of all define the namespace we are going to use

using System.IO;
using System.Net;

Now on the Click Event of the Button write code like below

private void btnDownload_Click(object sender, EventArgs e)
{
    WebClient wc = new WebClient();
    wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
    wc.DownloadFileAsync(new Uri(textBox1.Text.Trim()), @"c:\Users\Kirtan\Desktop\" + "Downloaded." + Path.GetExtension(textBox1.Text));
}

Here we take one webclient object and simply call download downloadfileasync() method and pass two parameters "url of file to download " and "location at we want to save the downloaded file"

Now main part comes that we want to show progress bar increment when downloading file declare event handler

wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);

And define Declared Event like below


public void wc_DownloadProgressChanged(Object sender, DownloadProgressChangedEventArgs e)

{

    pbDownloadStatus.Value = e.ProgressPercentage;

}


What it does assign the e.progresspercentage to the progressbar having maximum value 100.

Conclusion

This article explains about how to download file and showing its progress in progress bar.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor