热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

用C#Builder实现文件下载(转)

用C#Builder实现文件下载(转)[more]作者:徐长友一.概述:本文通过一个实例向大家介绍用C#Builder进行Inte.NET
用C# Builder实现文件下载 (转)[@more@]作者:徐长友

一.概述: 
   本文通过一个实例向大家介绍用C# Builder进行Inte.NET通讯编程的一些基本知识。我们知道.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 description for WinForm.
 /// 
 public class WinForm : System.Windows.Forms.Form
 {
 /// 
 /// Required designer variable.
 /// 
 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
 //
 }

 /// 
 /// Clean up any resources being used.
 /// 
 protected override void Dispose (bool disposing)
 {
 if (disposing)
 {
 if (components != null)
 {
 components.Dispose();
 }
 }
 base.Dispose(disposing);
 }

 #region Windows Form Designer generated code
 /// 
 /// Required method for Designer support - do not modify
 /// the contents of this method with the code editor.
 /// 
 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

 /// 
 /// The main entry point for the application.
 /// 
 [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运行程序,输入一下载地址,点“下载”按钮试试。

CSDN_Dev_Image_a%20href=2003-7-142350191.jpg" οnlοad="Javascript:if(this.width>screen.width-430)this.width=screen.width-430" align=center border=0>
四.结束语: 
   以上用一个简单的例子向大家展示了如何用C# Builder实现文件的下载,我们不难发现用C# Builder进行Internet通讯编程是非常方便的。在上面的程序中,我们仅仅用到了WebClient类的一些方法,而WebClient类不只是提供了文件下载的方法,还提供了文件上传等方法,有兴趣的读者不妨自己试试,查查帮助。
 程序中使用了多线程,这是因为WebClient类占有的资源校大,在下载文件会使整个窗口的显示不完整。如果不用多线程的话,下载文件的时候,你根本没法移动窗口或进行其它的一些操作。
 有兴趣的朋友可以到我的主页下载源码 http://yousoft.hi.com.cn

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-998917/,如需转载,请注明出处,否则将追究法律责任。

转:http://blog.itpub.net/10752043/viewspace-998917/



推荐阅读
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • YOLOv7基于自己的数据集从零构建模型完整训练、推理计算超详细教程
    本文介绍了关于人工智能、神经网络和深度学习的知识点,并提供了YOLOv7基于自己的数据集从零构建模型完整训练、推理计算的详细教程。文章还提到了郑州最低生活保障的话题。对于从事目标检测任务的人来说,YOLO是一个熟悉的模型。文章还提到了yolov4和yolov6的相关内容,以及选择模型的优化思路。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文讨论了如何在dotnet桌面(Windows)应用程序中添加图标。作者提到可以使用dotnet命令行工具与resource.rc文件一起使用来为标准.NET核心应用程序添加图标。作者还介绍了在创建控制台应用程序时如何编辑projeto1.csproj文件来添加图标。 ... [详细]
  • 全面介绍Windows内存管理机制及C++内存分配实例(四):内存映射文件
    本文旨在全面介绍Windows内存管理机制及C++内存分配实例中的内存映射文件。通过对内存映射文件的使用场合和与虚拟内存的区别进行解析,帮助读者更好地理解操作系统的内存管理机制。同时,本文还提供了相关章节的链接,方便读者深入学习Windows内存管理及C++内存分配实例的其他内容。 ... [详细]
  • 本文介绍了如何在Azure应用服务实例上获取.NetCore 3.0+的支持。作者分享了自己在将代码升级为使用.NET Core 3.0时遇到的问题,并提供了解决方法。文章还介绍了在部署过程中使用Kudu构建的方法,并指出了可能出现的错误。此外,还介绍了开发者应用服务计划和免费产品应用服务计划在不同地区的运行情况。最后,文章指出了当前的.NET SDK不支持目标为.NET Core 3.0的问题,并提供了解决方案。 ... [详细]
  • [echarts] 同指标对比柱状图相关的知识介绍及应用示例
    本文由编程笔记小编为大家整理,主要介绍了echarts同指标对比柱状图相关的知识,包括对比课程通过率最高的8个课程和最低的8个课程以及全校的平均通过率。文章提供了一个应用示例,展示了如何使用echarts制作同指标对比柱状图,并对代码进行了详细解释和说明。该示例可以帮助读者更好地理解和应用echarts。 ... [详细]
  • 抽空写了一个ICON图标的转换程序
    抽空写了一个ICON图标的转换程序,支持png\jpe\bmp格式到ico的转换。具体的程序就在下面,如果看的人多,过两天再把思路写一下。 ... [详细]
  • 第一步:PyQt4Designer设计程序界面该部分设计类同VisvalStudio内的设计,改下各部件的objectName!设计 ... [详细]
  • MVC中的自定义控件
    怎么样创建自定义控 ... [详细]
  • 本篇文章笔者在上海吃饭的时候突然想到的这段时间就有想写几篇关于返回系统的笔记,所以回家到之后就奋笔疾书的写出来发布了事先在网上找了很多方法,发现有 ... [详细]
author-avatar
小小的dream
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有