C#实现下载功能

C#实现下载功能

话不多说大家也应该明白

downstart类:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Windows.Forms;

namespace WindowsApplication1
{
    class downfile
    {
        /// <summary>
        /// 下载方法
        /// </summary>
        public void downstart()
        {
            WebClient web_cli = new WebClient();
            //获取一个 文件的长度
            WebRequest web_req = WebRequest.Create(filepath);    //发出对这个地址的请求
            WebResponse web_res = web_req.GetResponse();    //做出的响应
            long file_len = web_res.ContentLength;    //获取到文件的长度
            progressBar1.Maximum = (int)file_len;   //progressBar1 是进度条
            web_res.Close();
            //开始下载
            web_cli.DownloadData(filepath);  //加载资源
            Stream str_file = web_cli.OpenRead(filepath);  //以流方式读文件
            byte[]  mbyte= new byte[file_len];    //
            int len = (int)file_len;  //文件长度
            int i = 0;  
            while ((int)file_len > 0)    //如果有目标文件
            {
                int  n = str_file.Read(mbyte, i, len);            
                if (n == 0)  break;       //全部读完了退出循环
                progressBar1.Value = i;     //进度条显示进度
                i = i + n;     
                len = len - n;        
            }
            progressBar1.Value = progressBar1.Maximum;   
            FileStream fstrm = new FileStream(savepath  + System.IO.Path.GetFileName(filepath), FileMode.Create, FileAccess.Write);   
            fstrm.Write(mbyte,0,(int)file_len);//从流写到文件
            fstrm.Close();

        }
        /// <summary>
        /// 构造函数 初始化
        /// </summary>
        /// <param name="_filepath"></param>
        /// <param name="_savepath"></param>
        /// <param name="_progressBar1"></param>
        public downfile(string _filepath, string _savepath, ref ProgressBar _progressBar1)
        {
            this.filepath = _filepath;
            this.savepath = _savepath;
            this.progressBar1 = _progressBar1;
        }
        private string filepath;       //要下载的目标文件路径
        private string savepath;    //要保存位置的路径
        private ProgressBar progressBar1;

    }
}

程序form窗体需要至少两个控件:
progressBar  名为:progressBar1
button 名为:button1

button1的按钮调用事件:
        private void button3_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;   //关闭线程中操作控件出错
            downfile downdata = new downfile(@"http://pichd.gxnews.com.cn/uploadfiles/mp3/2007/6/14/200761420477680.mp3", @"d:/", ref progressBar1);  //构造函数 初始化 这里的参数需要根据程序的作用修改 这里为了方便直接给出了
            Thread th = new Thread(new ThreadStart(downdata.downstart));
            th.Start();  //线程执行
        }

写的很乱 如有问题 需要交流 请联系我 ……QQ:270469418

<script src="tag.php?action=relatetag&rtid=9218" type="text/javascript"></script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值