HANTAO官方博客

导航

统计

C# 断点续传的例子 单线程 ,支持按钮暂停和开始

网上没有一个完整的例子。我把网上的资源整理了一下,可以实现如下功能

1.支持http式下载,可以定义文件名,手工定义

2.实现下载--暂停--继续 功能。演示断点续传

3.实时查看当前下载的文件大小。

4.注释写的不好,几乎没有。

用  C# 2008 写的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Download
{
    public partial class Form1 : Form
    {
        Boolean fflag = false;

        public Form1()
        {
            InitializeComponent();
        }

 


        private void button1_Click(object sender, EventArgs e)
        {
            fflag = false;
            button1.Enabled = false;
            button2.Enabled = true;

            string strFileName = textBox2.Text;
            long lStartPos = 0;
            System.IO.FileStream fs;
            if (System.IO.File.Exists(strFileName))
            {
                fs = System.IO.File.OpenWrite(strFileName);
                lStartPos = fs.Length;

                fs.Seek(lStartPos, System.IO.SeekOrigin.Current);


            }

            else
            {
                fs = new System.IO.FileStream(strFileName, System.IO.FileMode.Create);
                lStartPos = 0;


            }

            lblFileSize.Text = lStartPos.ToString();
            try
            {
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(textBox1.Text.ToString());

                if (lStartPos > 0)

                    request.AddRange((int)lStartPos);

                System.IO.Stream ns = request.GetResponse().GetResponseStream();
                long ConLength = request.GetResponse().ContentLength;
                lblRange.Text = ((int)ConLength + (int)lStartPos).ToString();
              
                progressBar1.Maximum = (int)ConLength+(int)lStartPos ;
                progressBar1.Value = (int)lStartPos;

                byte[] nbytes = new byte[512];

                int nReadSize = 0;


                nReadSize = ns.Read(nbytes, 0, 512);

                lStartPos = lStartPos + nReadSize;

                while (nReadSize > 0)
                {
                    fs.Write(nbytes, 0, nReadSize);
                    nReadSize = ns.Read(nbytes, 0, 512);
                    //  System.EventHandler(button2_Click);
                    Application.DoEvents();
                    if (fflag == true)
                    break;
                    lStartPos = lStartPos + nReadSize;
                    lblFileSize.Text = lStartPos.ToString();
                    lblLeftRange.Text = (progressBar1.Maximum  - lStartPos).ToString();
                    progressBar1.Value = progressBar1.Value + nReadSize;
                   
                }

 

                fs.Close();
                ns.Close();
                if (lblLeftRange.Text == "0")
                { label6.Text = "下载完成"; }
                else
                { label6.Text = "暂停阶段"; }
               

            }

            catch (Exception ex)
            {
                fs.Close();

                label6.Text = ex.ToString();
                
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string strFileName = textBox2.Text;
            long lStartPos = 0;


            System.IO.FileStream fs;
            if (System.IO.File.Exists(strFileName))
            {
                fs = System.IO.File.OpenWrite(strFileName);
                lStartPos = fs.Length;

                fs.Seek(lStartPos, System.IO.SeekOrigin.Current);

                label6.Text = "文件存在!";
            }

            else
            {
                fs = new System.IO.FileStream(strFileName, System.IO.FileMode.Create);
                lStartPos = 0;
                label6.Text = "文件不存在!";

            }

            lblFileSize.Text = lStartPos.ToString();
            try
            {
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(textBox1.Text.ToString());

                

                System.IO.Stream ns = request.GetResponse().GetResponseStream();

               
                   lblRange.Text  = request.GetResponse().ContentLength.ToString ();

                fs.Close();
                ns.Close();
               

            }

            catch (Exception ex)
            {
                fs.Close();

                label6.Text = ex.ToString();

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.button2.Text == "Pause")
            {
               
                this.button2.Text = "Continue";
                fflag = true;

            }
            else
            {

                fflag = false ;
                this.button2.Text = "Pause";
                button1_Click(this ,e);
            }
        }

 

        private void button4_Click(object sender, EventArgs e)
        {
            string strFileName = textBox2.Text;
            button1.Enabled = true;
           
            progressBar1.Value = 0;

            if (System.IO.File.Exists(strFileName))
            {
                try
                {
                    System.IO.File.Delete(strFileName);
                    label6.Text = "删除文件成功!";
                }
                catch (System.IO.IOException ex)
                {

                    label6.Text = "删除文件失败!";

                }
            }
            else
            {

                label6.Text = "文件不存在!";
            }
        }

       
   
    }
}

 

posted on 2010-04-05 16:18 hantao 阅读(...) 评论(...) 编辑 收藏