using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace DownloadDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private WebClient client = new WebClient();
        Thread th;
        //自定义方法,提取文件名及路径
        public void BeginDown()
        {
            string url = this.txtbUrl.Text.Trim().ToString();
            int n = url.LastIndexOf("/");
            string address = url.Substring(0, n);
            string fileName = url.Substring(n + 1, url.Length - n - 1);
            string dir = this.txtbLocal.Text.Trim().ToString();
            string localName = dir + "\\" + fileName;
            WebRequest request = WebRequest.Create(url);
            this.toolStripStatusLabel1.Text = "开始下载......";
            client.DownloadFile(address, fileName);
            this.toolStripStatusLabel1.Text = "下载完毕。";
        }
        //下载按钮事件
        private void btDownload_Click(object sender, EventArgs e)
        {
            th = new Thread(new ThreadStart(BeginDown));
            th.Start();
        }

        private void btPath_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Application.StartupPath);
            if (File.Exists(@"E:\新建 文本文档.txt"))
            {
                File.Delete(@"E:\新建 文本文档.txt");
            }
            File.Copy(@"D:\新建 文本文档.txt", @"E:\新建 文本文档.txt");
        }
    }
}