使用WebClient实现下载

截图

  1<xmp>   
  2using System;   
  3using System.Drawing;   
  4using System.Collections;   
  5using System.ComponentModel;   
  6using System.Windows.Forms;   
  7using System.Data;   
  8using System.IO;   
  9using System.Net;   
 10using System.Threading;   
 11namespace DownLoad   
 12{   
 13/// <summary>   
 14/// Form1 的摘要说明。   
 15/// </summary>   
 16public class Form1 : System.Windows.Forms.Form   
 17{   
 18private System.Windows.Forms.Label label1;   
 19private System.Windows.Forms.Label label2;   
 20private System.Windows.Forms.TextBox txtURI;   
 21private System.Windows.Forms.TextBox txtPath;   
 22private System.Windows.Forms.Button btnSelect;   
 23private System.Windows.Forms.Button btnDownLoad;   
 24private System.Windows.Forms.StatusBar stBar;   
 25private System.Windows.Forms.FolderBrowserDialog folderBD;   
 26//使用WebClient的方法以实现文件的下载   
 27private WebClient client=new WebClient();   
 28/// <summary>   
 29/// 必需的设计器变量。   
 30/// </summary>   
 31private System.ComponentModel.Container components = null; 
 32
 33public Form1()   
 34{   
 35//   
 36// Windows 窗体设计器支持所必需的   
 37//   
 38InitializeComponent(); 
 39
 40//   
 41// TODO: 在 InitializeComponent 调用后添加任何构造函数代码   
 42//   
 43} 
 44
 45/// <summary>   
 46/// 清理所有正在使用的资源。   
 47/// </summary>   
 48protected override void Dispose( bool disposing )   
 49{   
 50if( disposing )   
 51{   
 52if (components != null)   
 53{   
 54components.Dispose();   
 55}   
 56}   
 57base.Dispose( disposing );   
 58} 
 59
 60#region Windows 窗体设计器生成的代码   
 61/// <summary>   
 62/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
 63/// 此方法的内容。   
 64/// </summary>   
 65private void InitializeComponent()   
 66{   
 67this.label1 = new System.Windows.Forms.Label();   
 68this.label2 = new System.Windows.Forms.Label();   
 69this.txtURI = new System.Windows.Forms.TextBox();   
 70this.txtPath = new System.Windows.Forms.TextBox();   
 71this.folderBD = new System.Windows.Forms.FolderBrowserDialog();   
 72this.btnSelect = new System.Windows.Forms.Button();   
 73this.btnDownLoad = new System.Windows.Forms.Button();   
 74this.stBar = new System.Windows.Forms.StatusBar();   
 75this.SuspendLayout();   
 76//   
 77// label1   
 78//   
 79this.label1.Location = new System.Drawing.Point(8, 24);   
 80this.label1.Name = "label1";   
 81this.label1.Size = new System.Drawing.Size(96, 23);   
 82this.label1.TabIndex = 0;   
 83this.label1.Text = "下载文件的URI:";   
 84//   
 85// label2   
 86//   
 87this.label2.Location = new System.Drawing.Point(16, 64);   
 88this.label2.Name = "label2";   
 89this.label2.Size = new System.Drawing.Size(88, 23);   
 90this.label2.TabIndex = 1;   
 91this.label2.Text = "本地保存路径:";   
 92//   
 93// txtURI   
 94//   
 95this.txtURI.Location = new System.Drawing.Point(112, 25);   
 96this.txtURI.Name = "txtURI";   
 97this.txtURI.Size = new System.Drawing.Size(208, 21);   
 98this.txtURI.TabIndex = 2;   
 99this.txtURI.Text = "";   
100//   
101// txtPath   
102//   
103this.txtPath.Location = new System.Drawing.Point(112, 65);   
104this.txtPath.Name = "txtPath";   
105this.txtPath.Size = new System.Drawing.Size(208, 21);   
106this.txtPath.TabIndex = 3;   
107this.txtPath.Text = "";   
108//   
109// btnSelect   
110//   
111this.btnSelect.Location = new System.Drawing.Point(328, 64);   
112this.btnSelect.Name = "btnSelect";   
113this.btnSelect.TabIndex = 4;   
114this.btnSelect.Text = "选择路径...";   
115this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);   
116//   
117// btnDownLoad   
118//   
119this.btnDownLoad.Location = new System.Drawing.Point(112, 104);   
120this.btnDownLoad.Name = "btnDownLoad";   
121this.btnDownLoad.TabIndex = 5;   
122this.btnDownLoad.Text = "下载";   
123this.btnDownLoad.Click += new System.EventHandler(this.btnDownLoad_Click);   
124//   
125// stBar   
126//   
127this.stBar.Location = new System.Drawing.Point(0, 184);   
128this.stBar.Name = "stBar";   
129this.stBar.Size = new System.Drawing.Size(424, 22);   
130this.stBar.TabIndex = 6;   
131//   
132// Form1   
133//   
134this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);   
135this.ClientSize = new System.Drawing.Size(424, 206);   
136this.Controls.Add(this.stBar);   
137this.Controls.Add(this.btnDownLoad);   
138this.Controls.Add(this.btnSelect);   
139this.Controls.Add(this.txtPath);   
140this.Controls.Add(this.txtURI);   
141this.Controls.Add(this.label2);   
142this.Controls.Add(this.label1);   
143this.Name = "Form1";   
144this.Text = "Form1";   
145this.ResumeLayout(false); 
146
147}   
148#endregion 
149
150/// <summary>   
151/// 应用程序的主入口点。   
152/// </summary>   
153[STAThread]   
154static void Main()   
155{   
156Application.Run(new Form1());   
157} 
158
159private void btnSelect_Click(object sender, System.EventArgs e)   
160{   
161if(folderBD.ShowDialog()==DialogResult.Cancel)   
162{   
163return;   
164}   
165txtPath.Text=folderBD.SelectedPath;   
166} 
167
168private void btnDownLoad_Click(object sender, System.EventArgs e)   
169{   
170//开启线程,以改善用户体验.   
171Thread th=new Thread(new ThreadStart(StartDownLoad));   
172//th.Name="New DownLoad";   
173th.Start();   
174}   
175private void StartDownLoad()   
176{   
177string url=string.Empty;   
178string fileName=string.Empty;   
179int n=0;   
180string path=string.Empty; 
181
182btnDownLoad.Enabled=false;   
183url=txtURI.Text;   
184n=url.LastIndexOf('/');   
185fileName=url.Substring(n+1);   
186path=txtPath.Text+" \\\"+fileName ; 
187
188try   
189{   
190WebRequest request=WebRequest.Create(url);   
191}   
192catch(WebException ex)   
193{   
194MessageBox.Show(ex.Message,"Error");   
195} 
196
197try   
198{   
199stBar.Text="开始下载文件...";   
200client.DownloadFile(url,path);   
201stBar.Text="下载完毕,文件已经保存到"+path.ToString();   
202}   
203catch(WebException ex)   
204{   
205MessageBox.Show(ex.Message,"Error");   
206stBar.Text=string.Empty;   
207}   
208finally   
209{   
210btnDownLoad.Enabled=true;   
211}   
212}   
213}   
214}   
215  
216</xmp>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus