Download File With Progressbar using C#.

No.of Views1516
Bookmarked0 times
Downloads 
Votes0
By  kirtan007   On  16 Feb 2010 00:02:48
Tag : CSharp , Windows Forms
Download File With Progressbar using C#.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 Introduction

Some time when download a file from the internet using webclient we need to show progressbar to the user indecating how much task is accomlished . this can be very difficult and cumbersome when using WebClient.DownloadFil() method because it freez the interface and we need to do extra coding by thread to show progressbar

Here i m going to tell 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 interface like below

Image Loading....



take a label , Text Box , Progressbar and a Button .

Now in Code First of all define the namespace we are going to use

{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System.IO;
using System.Net;

{/codecitation}

Now on the Click Event of the Button

Write code like below


{codecitation class="brush: csharp; gutter: true;" width="650px"}


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));

}

{/codecitation}


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 progressbar increment when downloading file

Declare Event handler

{codecitation class="brush: csharp; gutter: true;" width="650px"}


wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);

{/codecitation}

and define Declared Event like below

{codecitation class="brush: csharp; gutter: true;" width="650px"}


public void wc_DownloadProgressChanged(Object sender, DownloadProgressChangedEventArgs e)
{
pbDownloadStatus.Value = e.ProgressPercentage;

}


{/codecitation}

What it does Assign the e.ProgressPercentage to the progressbar having MAximum Value 100.

Thats it .

Conclusion

Article Explaines about How to Download File and Showing its Progress in Progressbar .

Download Project Files

Thank you .

About the Author



Kirtan Patel
- Bachelor of Computer Application .
- File Handling with C#
- Registry Programming With C#.
- Knowledge of Virus Programming .
- Working With third party controls like Telerik and Component Factory .
- P/invoke Win32 API.
- SQL Server Database Applications
- Client/Server Database Programming
Expertise in : C# , Asp.net ,Vb.net , SQL Server , Designing Elegant look Web interface
Familiar With : C/C++ , Core Java , Java Script , Dream weaver templates , fire Works ,PHP ,Photo shop
and Graphics
Databases: Microsoft SQL Server 2005 , Microsoft Access and Some MySQL
Occupation: Student


 
Sign Up to vote for this article
 
About Author
 
kirtan007
Occupation-
Company-
Member Type-Senior
Location-Not Provided
Joined date-02 Jul 2009
Home Page-http://kirtan.uni.cc
Blog Page-
He completed his Bachelor of Computer Application from Gujarat University 2009 .He is doing Master of Computer Application from Gujarat Technological University right now .. His area of Interests are Web Hacking , C# .net Windows form ,asp.net , WPF ,Silverlight ,SQL Server and Some PHP.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top