c# 文件下载 进度及速度

mac2022-06-30  22

 

                                      

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); //设置委托用来更新主界面        private int totalseconds = 0; //总用时        private updateData UIDel; //        public Form1()        {            InitializeComponent();            buffer = new byte[4096];                    }

        private void button1_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("下载完毕");            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 timer1_Tick(object sender, EventArgs e)        {            this.label2.Text = ((downlength - lastlength) / 1024) + "KB/S";            lastlength = downlength;            totalseconds++;        }    }}

转载于:https://www.cnblogs.com/webman/archive/2006/11/15/561372.html

相关资源:c#写的多线程Http下载,经测试下载速度还可以
最新回复(0)