• 网络学院
  • IT资讯
  • 操作系统
  • 网络技术
  • 软件应用
  • 办公软件
  • 编程技术
  • 网站架设
  • 数据库类
  • 平面设计
  • 多媒体类
  • 游戏资讯
  • 教学论文
  • 认证考试
用C#
  站点:
  • 首 页
  • 最新软件
  • 文章教程
  • 国内软件
  • 国外软件
  • 绿色软件
  • 源码下载
  • 字体下载
用C# Builder实现文件下载程序
软件发布 用C# Builder实现文件下载程序
网络软件 系统工具 应用软件 联络聊天 图形图像 多媒体类 行业软件 游戏娱乐 编程开发 安全相关 教育教学 数码软件 绿软下载
热门软件: QQ 瑞星 pplive e话通 木马克星 千千静听 office2000 五笔字根 Photoshop 视频分割
返回文章教程首页 >> 文章首页 >> 编程技术 >> C#教程 >> 用C# Builder实现文件下载程序

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

添加时间: 2007-7-13 2:00:11  作者: C#教程  阅读次数:76   来源: http://www.d9soft.com

       

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

 

上下文章:

 

上一篇文章: C#中改变显示器的分辨率 下一篇文章: 用C# Builder生成PDF文件

相关文章:

  • 飞速在线看视频 傲盾加速帮你实现速度感受
  • 新蠕虫伪装成文件夹图标,让你防不胜防
  • Vista找不到Config文件夹的解决办法
  • Vista系统的官方优化指南文档下载
  • 用端口碰撞技术实现服务器远程管理

相关软件:

  • MP3搜索下载快车 V4.5 绿色特别版
  • 瑞星个人防火墙2008下载版(免费1年) 20.57.12
  • 文件管理精灵 V3.21
  • 网站全额下载器 2002
  • AGIsBuilder V1.0 汉化版
  • FTP远程文件同步更新程序 1.0.0.0

 

快速导航

  • 网络学院
  • 精品汇聚
  • 字体下载
  • 教程下载
  • ASP源码
  • PHP源码
  • Net源码
  • JSP 源码

编程技术分类导航

  • ASP & ASP.NET教程
  • PHP教程
  • JSP教程
  • C/C++教程
  • VB & VB.NET教程
  • VC教程
  • Delphi教程
  • BCB教程
  • VFP教程
  • PB教程
  • JAVA教程
  • XML教程
  • C#教程
  • CGI教程

本类经典文章推荐

  • ADO.NET的开发场景及传统ADO的处理
  • 利用Visual C#实现Window管道技术
  • C#取得汉字的拼音的首字母
  • 使用C#编写DES加密程序的framework
  • Visual C#编写3D游戏框架示例
  • 用C#和本地Windows API操纵系统菜...
  • 在C#程序设计中使用Win32类库
  • Visual C#中调用Windows服务初探
  • 如何在C#的WinForm中制作饼状图和...
  • C#中实现DataGrid双向排序

C#教程阅读排行

  • 如何在C#的WinForm中制作饼状图和...
  • 浅析C#中图形编程
  • 用C#和本地Windows API操纵系统菜...
  • 彻底剖析C# 2.0泛型类的创建和使用
  • 使用C#编写DES加密程序的framework
  • Visual C#编写3D游戏框架示例
  • Visual C#中调用Windows服务初探
  • C#如何在Form中嵌入并且操作Excel...
  • Visual C# 2005实现控件中捕获按键
  • C#中利用mediaplayer打造mp3播放器

编程技术阅读总排行

  • VB入门教程之一
  • Java连接数据库实例
  • 第二章 PowerBuilder 入门之创建新...
  • VC++之List Box/Check List Box控...
  • 第一章 什么是PowerBuilder
  • VC++ List Ctrl控件
  • VC++ Combo Box/Combo Box Ex控件
  • 学C++不得不看的一篇文章
  • VB入门教程之二
  • VC++之Button控件

广告位置

字母检索 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 回到顶部

关于我们 | 版权声明 | 免责条款 | 广告联系 | 软件发布 | 下载帮助 | 下载排行 | 网站地图 | 特别鸣谢 | 友情连接

copyright; 2005-2008 D9soft.com 第九软件网 版权所有