FrankBoy 发表于 2016-6-13 11:56

.NET之下载器的制作 System.NET类

本帖最后由 FrankBoy 于 2016-6-13 12:46 编辑

最近在研究C#的Post ,闲暇之余写了一个简单的下载器与大家共同交流。
类: System.Net.Client

编译环境:vs2013   .Net Framework 4.0
贴出代码的意义纯属交流,希望能共获提升。
贴出的代码均已意义注释,如果还有什么不懂请帖子内回复,看到会一一回复。源码简单修改下封装为类,可以方便大家以后的使用。

static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.png


源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;


namespace 下载工具
{
    public partial class FrmMain : Form
    {
      public FrmMain()
      {
            InitializeComponent();
      }


      private void FrmMain_Load(object sender, EventArgs e)
      {
      }
      private void button1_Click(object sender, EventArgs e)
      {
            string url = txtDownLoad.Text.Trim();    //获取编辑框内容并去除首尾空格赋值给url
            WebClient webDown = new WebClient();   // 实例化一个 WebClient 对象
            if (!string.IsNullOrWhiteSpace(url))   //判断 url 的内容不是null或者为空
            {
                try                                 //获取输入的内容为无效的地址则会抛出异常
                {
                  //使用异步的下载方式下载文件
                  webDown.DownloadFileAsync(new Uri(url), Application.StartupPath + "\\" + Path.GetFileName(url));
                }
                catch (Exception ex)                     
                {
                  MessageBox.Show(ex.Message);         //使用消息框显示出异常
                }
                webDown.DownloadFileCompleted += webDown_DownloadFileCompleted;      //文件下载完成时执行的事件
                webDown.DownloadProgressChanged += webDown_DownloadProgressChanged;    //文件开始下载时发生
            }
      }


      void webDown_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
      {
            if (DialogResult.OK == MessageBox.Show("下载完成"))   //判断弹出的信息框点击确定为真
            {
                toolStripProgressBar1.Value = 0;      //进度条的Value值为0
            }
      }


      void webDown_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
      {
            toolStripProgressBar1.Value = e.ProgressPercentage;   //e.ProgressPercentage获取到下载进度的百分比赋值给进度条
      }


    }
}
https://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.pnghttps://www.52hb.com/static/image/hrline/line8.png


806226268 发表于 2016-6-13 13:56

软件看起来挺强悍的

莫离 发表于 2016-6-13 18:00

这东西一定要支持。

十八 发表于 2016-6-13 23:01

             楼主加油

coverme 发表于 2016-6-15 12:32

这东西一定要支持。

TaiLan 发表于 2016-6-16 23:03

楼主好人呀

ronle 发表于 2016-6-27 09:53

又学会了一招,收藏一份

freesilo 发表于 2018-8-8 22:49


感谢分享,最近在研究这个,收藏有空再慢慢看。
页: [1]
查看完整版本: .NET之下载器的制作 System.NET类