download,C#, HttpWebRequest,FolderBrowserDialog

來源:互聯網
上載者:User
使用WebClient實現下載
http://www.zj55.net/article/html/s816/34566.htm


<xmp>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net;
using System.Threading;
namespace DownLoad
{
 /// <summary>
 /// Form1 的摘要說明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.TextBox txtURI;
  private System.Windows.Forms.TextBox txtPath;
  private System.Windows.Forms.Button btnSelect;
  private System.Windows.Forms.Button btnDownLoad;
  private System.Windows.Forms.StatusBar stBar;
  private System.Windows.Forms.FolderBrowserDialog folderBD;
  //使用WebClient的方法以實現檔案的下載
  private WebClient client=new WebClient();
  /// <summary>
  /// 必需的設計器變數。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 表單設計器支援所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
   //
  }

  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 表單設計器產生的程式碼
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.txtURI = new System.Windows.Forms.TextBox();
   this.txtPath = new System.Windows.Forms.TextBox();
   this.folderBD = new System.Windows.Forms.FolderBrowserDialog();
   this.btnSelect = new System.Windows.Forms.Button();
   this.btnDownLoad = new System.Windows.Forms.Button();
   this.stBar = new System.Windows.Forms.StatusBar();
   this.SuspendLayout();
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(8, 24);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(96, 23);
   this.label1.TabIndex = 0;
   this.label1.Text = "下載檔案的URI:";
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(16, 64);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(88, 23);
   this.label2.TabIndex = 1;
   this.label2.Text = "本地儲存路徑:";
   //
   // txtURI
   //
   this.txtURI.Location = new System.Drawing.Point(112, 25);
   this.txtURI.Name = "txtURI";
   this.txtURI.Size = new System.Drawing.Size(208, 21);
   this.txtURI.TabIndex = 2;
   this.txtURI.Text = "";
   //
   // txtPath
   //
   this.txtPath.Location = new System.Drawing.Point(112, 65);
   this.txtPath.Name = "txtPath";
   this.txtPath.Size = new System.Drawing.Size(208, 21);
   this.txtPath.TabIndex = 3;
   this.txtPath.Text = "";
   //
   // btnSelect
   //
   this.btnSelect.Location = new System.Drawing.Point(328, 64);
   this.btnSelect.Name = "btnSelect";
   this.btnSelect.TabIndex = 4;
   this.btnSelect.Text = "選擇路徑...";
   this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
   //
   // btnDownLoad
   //
   this.btnDownLoad.Location = new System.Drawing.Point(112, 104);
   this.btnDownLoad.Name = "btnDownLoad";
   this.btnDownLoad.TabIndex = 5;
   this.btnDownLoad.Text = "下載";
   this.btnDownLoad.Click += new System.EventHandler(this.btnDownLoad_Click);
   //
   // stBar
   //
   this.stBar.Location = new System.Drawing.Point(0, 184);
   this.stBar.Name = "stBar";
   this.stBar.Size = new System.Drawing.Size(424, 22);
   this.stBar.TabIndex = 6;
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(424, 206);
   this.Controls.Add(this.stBar);
   this.Controls.Add(this.btnDownLoad);
   this.Controls.Add(this.btnSelect);
   this.Controls.Add(this.txtPath);
   this.Controls.Add(this.txtURI);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 應用程式的主進入點。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void btnSelect_Click(object sender, System.EventArgs e)
  {
   if(folderBD.ShowDialog()==DialogResult.Cancel)
   {
    return;
   }
   txtPath.Text=folderBD.SelectedPath;
  }

  private void btnDownLoad_Click(object sender, System.EventArgs e)
  {
   //開啟線程,以改善使用者體驗.
   Thread th=new Thread(new ThreadStart(StartDownLoad));
   //th.Name="New DownLoad";
   th.Start();
  }
  private void StartDownLoad()
  {
   string url=string.Empty;
   string fileName=string.Empty;
   int n=0;
   string path=string.Empty;

   btnDownLoad.Enabled=false;
   url=txtURI.Text;
   n=url.LastIndexOf('/');
   fileName=url.Substring(n+1);
   path=txtPath.Text+"
\\"+fileName;

   try
   {
    WebRequest request=WebRequest.Create(url);
   }
   catch(WebException ex)
   {
    MessageBox.Show(ex.Message,"Error");
   }

   try
   {
    stBar.Text="開始下載檔案...";
    client.DownloadFile(url,path);
    stBar.Text="下載完畢,檔案已經儲存到"+path.ToString();
   }
   catch(WebException ex)
   {
    MessageBox.Show(ex.Message,"Error");
    stBar.Text=string.Empty;
   }
   finally
   {
    btnDownLoad.Enabled=true;
   }
  }
 }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.