科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件用C# Builder实现文件下载程序

用C# Builder实现文件下载程序

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文通过一个实例向大家介绍用C# Builder进行Internet通讯编程的一些基本知识

作者:徐长友 来源:悠游在线 2007年11月14日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
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类占有的资源校大,在下载文件会使整个窗口的显示不完整。如果不用多线程的话,下载文件的时候,你根本没法移动窗口或进行其它的一些操作。

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章