C # file download progress and speed

Source: Internet
Author: User

Visual Studio 2005 + WINXP SP2

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. IO;
Using Microsoft. Win32;
Using system. runtime. interopservices;
Using system. net;
Using system. net. Sockets;

Namespace commonlibrary
{
Public partial class form1: Form
{
Private webrequest httprequest;
Private webresponse httpresponse;
Private byte [] buffer;
Private thread downloadthread;
Stream ns;
Private string filename = @ "D: \ hello1.mp3 ";
Private filestream FS;
Private long length;
Private long downlength = 0;
Private long lastlength = 0;
Public Delegate void updatedata (string value); // sets the delegate to update the main interface
Private int totalseconds = 0; // total time used
Private updatedata uidel ;//
Public form1 ()
{
Initializecomponent ();
Buffer = new byte [4096];

}

Private void button#click (Object sender, eventargs E)
{
Httprequest = webrequest. Create (this. textbox1.text );
Httpresponse = httprequest. getresponse ();
// MessageBox. Show (httpresponse. contentlength. tostring ());
Length = httpresponse. contentlength;
This. progressbar1.maximum = (INT) length;
This. progressbar1.minimum = 0;
Downloadthread = new thread (New threadstart (downloadfile ));
// Showthread = new thread (New threadstart (show ));
FS = new filestream (filename, filemode. openorcreate, fileaccess. Write );
Downloadthread. Start ();
This. timer1.enabled = true;
// Showthread. Start ();
}
Private void downloadfile ()
{

NS = httpresponse. getresponsestream ();

Int I;
Uidel = new updatedata (updateui );
While (I = NS. Read (buffer, 0, buffer. Length)> 0)
{

Downlength + = I;
String value = downlength. tostring ();
This. Invoke (uidel, value );
FS. Write (buffer, 0, I );
}
MessageBox. Show (" Download Completed ");
This. timer1.enabled = false;
This. label3.text = (length/(1024 * totalseconds) + "kb/s ";
}
/* Private void show ()
{
Uidel = new updatedata (updateui );
Int value = 0;
While (value <= 100)
{
This. progressbar1.value = value;
Value ++;
}
}*/
Void updateui (string value)
{
This. label1.text = value;
This. progressbar1.value = int32.parse (value );
}

Private void timereffectick (Object sender, eventargs E)
{
This. label2.text = (downlength-lastlength)/1024) + "kb/s ";
Lastlength = downlength;
Totalseconds ++;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.