程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> 用C# Builder實現文件下載程序

用C# Builder實現文件下載程序

編輯:C#基礎知識

  一.概述:

  本文通過一個實例向大家介紹用C# Builder進行Internet通訊編程的一些基本知識。我們知道.Net類包含了請求/響應層、應用協議層、傳輸層等層次。在本程序中,我們運用了位於請求/響應層的WebRequest類以及WebClient類等來實現高抽象程度的Internet通訊服務。本程序的功能是完成文件的下載。

  二.實現原理:

  程序實現的原理比較簡單,主要用到了WebClient類和FileStream類。其中WebClient類處於System.Net名字空間中,該類的主要功能是提供向URI標識的資源發送數據和從URI標識的資源接收數據的公共方法。我們利用其中的DownFile()方法將文件下載到本地。然後用FileStream類的實例對象以數據流的方式將文件數據寫入本地文件。這樣就完成了文件的下載。

  三.實現步驟:

  1.首先,打開C# Builder,File->New->C# Application,Name這裡我們設為"download"。

  2.主界面的設置。text設為“文件下載”,StartPosition設為CenterScreen,MaximizeBox設為False,我們在主窗體上添加如下控件:兩個標簽控件label1,label2、一個文本框控件textBox1、一個按鈕控件button1以及一個進度條控件progressBar1。

  label1:text為URL;Label2:text為下載進度;textBox1:text設為空;button1:text設為下載;

  3.程序的編碼

  

//過程downfile,用於完成文件的下載
private void downfile()
{
string FileName;
WebClient DownFile=new WebClient();
long fbytes;
if (textBox1.Text!="")
{
saveFileDialog1.ShowDialog();
FileName=saveFileDialog1.FileName;
if(FileName!= "")
{
//取得文件大小
WebRequest wr_request=WebRequest.Create(textBox1.Text);
WebResponse wr_response=wr_request.GetResponse();
fbytes=wr_response.ContentLength;
progressBar1.Maximum=(int)fbytes;
progressBar1.Step=1;
wr_response.Close();
//開始下載數據
DownFile.DownloadData(textBox1.Text);
Stream strm = DownFile.OpenRead(textBox1.Text);
StreamReader reader = new StreamReader(strm);
byte[] mbyte = new byte[fbytes];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
while(fbytes>0)
{
int m = strm.Read(mbyte,startmbyte,allmybyte);
if(m==0) break;
startmbyte+=m;
allmybyte-=m;
progressBar1.value+=m;
}
FileStream fstrm = new FileStream(FileName,FileMode.OpenOrCreate,FileAccess.Write);
fstrm.Write(mbyte,0,startmbyte);
strm.Close();
fstrm.Close();
progressBar1.value=progressBar1.Maximum;
}
} else
{
MessageBox.Show("沒有輸入要下載的文件!");
}
}
//雙擊“下載”按鈕,輸入以下代碼:
Thread th = new Thread(new ThreadStart(downfile));
th.Start();
//完整的代碼如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
using System.Threading;
namespace download
{
/// <summary>
/// Summary description for WinForm.
/// </summary>
public class WinForm : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ProgressBar progressBar1;
public WinForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.label2 = new System.Windows.Forms.Label();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(40, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(40, 16);
this.label1.TabIndex = 0;
this.label1.Text = "URL:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(72, 36);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(256, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(256, 120);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "下載";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 80);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 23);
this.label2.TabIndex = 3;
this.label2.Text = "下載進度:";
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(72, 80);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(256, 16);
this.progressBar1.TabIndex = 4;
//
// WinForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(360, 173);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.label2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.Name = "WinForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "文件下載";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new WinForm());
}
private void downfile()
{
string FileName;
WebClient DownFile=new WebClient();
long fbytes;
if (textBox1.Text!="")
{
saveFileDialog1.ShowDialog();
FileName=saveFileDialog1.FileName;
if(FileName!= "")
{
//取得文件大小
WebRequest wr_request=WebRequest.Create(textBox1.Text);
WebResponse wr_response=wr_request.GetResponse();
fbytes=wr_response.ContentLength;
progressBar1.Maximum=(int)fbytes;
progressBar1.Step=1;
wr_response.Close();
//開始下載數據
DownFile.DownloadData(textBox1.Text);
Stream strm = DownFile.OpenRead(textBox1.Text);
StreamReader reader = new StreamReader(strm);
byte[] mbyte = new byte[fbytes];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
while(fbytes>0)
{
int m = strm.Read(mbyte,startmbyte,allmybyte);
if(m==0) break;
startmbyte+=m;
allmybyte-=m;
progressBar1.value+=m;
}
FileStream fstrm = new FileStream(FileName,FileMode.OpenOrCreate,FileAccess.Write);
fstrm.Write(mbyte,0,startmbyte);
strm.Close();
fstrm.Close();
progressBar1.value=progressBar1.Maximum;
}
} else
{
MessageBox.Show("沒有輸入要下載的文件!");
}
}
private void button1_Click(object sender, System.EventArgs e)
{
Thread th = new Thread(new ThreadStart(downfile));
th.Start();
}
}
}

  4.按F9運行程序,輸入一下載地址,點“下載”按鈕試試。

  

  四.結束語:

  以上用一個簡單的例子向大家展示了如何用C# Builder實現文件的下載,我們不難發現用C# Builder進行Internet通訊編程是非常方便的。在上面的程序中,我們僅僅用到了WebClient類的一些方法,而WebClient類不只是提供了文件下載的方法,還提供了文件上傳等方法,有興趣的讀者不妨自己試試,查查幫助。

  程序中使用了多線程,這是因為WebClient類占有的資源校大,在下載文件會使整個窗口的顯示不完整。如果不用多線程的話,下載文件的時候,你根本沒法移動窗口或進行其它的一些操作。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved