WebClient.DownloadFileAsync实现下载网络数据-简单的下载软件

本软件利用WebClient.DownloadFileAsync实现下载网络数据。很简单的一个程序。

 

源程序:

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;
using System.Net;
using System.Net.Sockets;
using System.IO;


namespace FlyDown
{
    public partial class Form1 : Form
    {
        Timer timer = new Timer();
        long timecount = 0;
        public Form1()
        {
            InitializeComponent();
            
            timer.Enabled = false;
            timer.Interval = 1000;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        
        private  void button1_Click(object sender, EventArgs e)
        {
            timer.Enabled = true;
            timer .Tick +=new EventHandler(timer_Tick);
            TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);
            using (WebClient wc = new WebClient())
            {
                
                if (textBox1.Text.Trim() == "")
                    MessageBox.Show("请输入下载路径");
                else if (textBox2.Text.Trim() == "")
                    MessageBox.Show("请输入文件存储名");
                else
                {

                    try
                    {
                        wc.Proxy = null;
                        Uri address = new Uri(textBox1.Text.ToString());
                        //调用DownloadFile方法下载文件
                        // wc.DownloadFile(textBox1.Text.ToString(), textBox2.Text.ToString());

                        //调用DownloadFileAsync异步下载文件
                        wc.DownloadFileAsync(address, textBox2.Text.ToString());
                        wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
                        //下载完成的响应事件
                        wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);

                        //if(File .Exists (textBox2 .Text .ToString ()))
                        //    {
                        //        TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                        //        TimeSpan ts=ts2.Subtract (ts1 ).Duration();

                        //        MessageBox.Show("文件下载完成.下载用时:" + ts.Hours.ToString() + "小时" +
                        //            ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒");
                        //    }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            
        }
        private void timer_Tick(object o, EventArgs e)
        {
            timecount++;
        }
        //提示下载完成,并显示下载用时
        private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            
            MessageBox.Show("下载完成,下载用时:"+timecount+"秒" );
        }
        //定义进度条响应事件
        private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(textBox2.Text.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(Application.ExecutablePath+"+"+Directory .GetCurrentDirectory ()+"+"+Application .StartupPath);
            System.Diagnostics.Process.Start(Application.StartupPath);
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值