下载远程文件到本地

鐣岄潰
 
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.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();
        }
    }
}
相关文章
相关标签/搜索