导读:
  这样,程序主体部分的代码已经完成了,不过要完成全部程序还需要一些工作。由于在程序接收网络文件数据的时候运用到了while循环体,这样会很占程序资源,表现的形式就是主窗体不能自由移动。为了解决这个问题,我们在程序中用到了多线程机制。我们在响应按钮的事件中新建一个线程,该线程就是用来实现网络文件下载功能的。如此,文件下载的线程和程序主线程并存,共享进程资源,使得程序顺畅运行。这样,我们在按钮控件的消息响应函数里添加如下代码:
  Thread th = new Thread(new ThreadStart(StartDownload)); th.Start();
  该线程的实现函数就是StartDownload(),而上面介绍的那些代码就是这个函数的主体部分。
  最后,因为程序中运用到了WebRequest、WebClient、FileStream、Thread等类,所以最重要的就是在程序的开始处添加如下名字空间:
  using System.Net; using System.IO; using System.Threading;
  下面就是程序的源代码:
  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 MyGetCar { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox srcAddress; private System.Windows.Forms.TextBox tarAddress; private System.Windows.Forms.StatusBar statusBar; private System.Windows.Forms.Button Start; private WebClient client = new WebClient(); /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.srcAddress = new System.Windows.Forms.TextBox(); this.tarAddress = new System.Windows.Forms.TextBox(); this.statusBar = new System.Windows.Forms.StatusBar(); this.Start = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(8, 32); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(72, 23); this.label1.TabIndex = 0; this.label1.Text = "文件地址:"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // label2 // this.label2.Location = new System.Drawing.Point(8, 72); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(72, 23); this.label2.TabIndex = 1; this.label2.Text = "另存到:"; this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // srcAddress // this.srcAddress.Location = new System.Drawing.Point(80, 32); this.srcAddress.Name = "srcAddress"; this.srcAddress.Size = new System.Drawing.Size(216, 21); this.srcAddress.TabIndex = 2; this.srcAddress.Text = ""; // // tarAddress // this.tarAddress.Location = new System.Drawing.Point(80, 72); this.tarAddress.Name = "tarAddress"; this.tarAddress.Size = new System.Drawing.Size(216, 21); this.tarAddress.TabIndex = 3; this.tarAddress.Text = ""; // // statusBar // this.statusBar.Location = new System.Drawing.Point(0, 151); this.statusBar.Name = "statusBar"; this.statusBar.Size = new System.Drawing.Size(312, 22); this.statusBar.TabIndex = 4; // // Start // this.Start.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.Start.Location = new System.Drawing.Point(216, 112); this.Start.Name = "Start"; this.Start.Size = new System.Drawing.Size(75, 24); this.Start.TabIndex = 5; this.Start.Text = "开始下载"; this.Start.Click += new System.EventHandler(this.Start_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(312, 173); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.Start, this.statusBar, this.tarAddress, this.srcAddress, this.label2, this.label1}); this.MaximizeBox = false; this.Name = "Form1"; this.Text = "文件下载器"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void StartDownload() { Start.Enabled = false; string URL = srcAddress.Text; int n = URL.LastIndexOf('/'); string URLAddress = URL.Substring(0,n); string fileName = URL.Substring(n+1,URL.Length-n-1); string Dir = tarAddress.Text; string Path = Dir+'\\'+fileName; try { WebRequest myre=WebRequest.Create(URLAddress); } catch(WebException exp) { MessageBox.Show(exp.Message,"Error"); } try { statusBar.Text = "开始下载文件..."; client.DownloadFile(URLAddress,fileName); Stream str = client.OpenRead(URLAddress); StreamReader reader = new StreamReader(str); byte[] mbyte = new byte[100000]; int allmybyte = (int)mbyte.Length; int startmbyte = 0; statusBar.Text = "正在接收数据..."; while(allmybyte>0) { int m = str.Read(mbyte,startmbyte,allmybyte); if(m==0) break; startmbyte+=m; allmybyte-=m; } FileStream fstr = new FileStream(Path,FileMode.OpenOrCreate,FileAccess.Write); fstr.Write(mbyte,0,startmbyte); str.Close(); fstr.Close(); statusBar.Text = "下载完毕!"; } catch(WebException exp) { MessageBox.Show(exp.Message,"Error"); statusBar.Text = ""; } Start.Enabled = true; } private void Start_Click(object sender, System.EventArgs e) { Thread th = new Thread(new ThreadStart(StartDownload)); th.Start(); } } }
  程序完毕,运行程序图示如下:

本文转自
http://study.qqcf.com/web/224/23978.htm

用C#实现文件下载器(2)相关推荐

  1. python写一个文件下载器_Python3使用TCP编写一个简易的文件下载器

    原标题:Python3使用TCP编写一个简易的文件下载器 利用Python3来实现TCP协议,和UDP类似.UDP应用于及时通信,而TCP协议用来传送文件.命令等操作,因为这些数据不允许丢失,否则会造 ...

  2. 使用IntentService给自己的Android应用写一个文件下载器。

    接着上一篇的http://www.cnblogs.com/zhengxt/p/3657833.html,当我们想给自己的APP写一个文件下载器时,可以用重写IntentService来实现. 使用In ...

  3. python写一个文件下载器_python使用tcp实现一个简单的下载器

    上一篇中介绍了tcp的流程,本篇通过写一个简单的文件下载器程序来巩固之前学的知识. 文件下载器的流程如下: 客户端: 输入目标服务器的ip和port 输入要下载文件的名称 从服务器下载文件保存到本地 ...

  4. 【多线程编程学习】java多线程基于数据分割的大文件下载器

    文章目录 代码:基于数据分割的大文件下载器 作为包装的存储对象类: 主文件下载类: 子任务下载类: 处理缓存: 启动类: 数据分割思想产生的问题 代码来自书籍<java多线程编程实战指南> ...

  5. python文件下载器代码_GitHub - applechi/pythonCollection: python代码集合(文件下载器、pdf合并、极客时间专栏下载、掘金小册下载、新浪微博爬虫等)...

    json2mysql 这次更新了将一个json文件中的数据导入到mysql的脚本. 是用nodejs写的. 对应的文件是tomysql.js 有兴趣的同志可以研究下. pythonCollection ...

  6. renameto 阻塞_打造简化版文件下载器

    一. 前言 Executors 是一种典型的生产者 - 消费者模式, java中的线程池是运用场景最多的并发框架,几乎所有需要异步或并发执行任务的程序都可以使用线程池.线程池就是将线程进行池化,需要运 ...

  7. 使用网络TCP搭建一个简单文件下载器

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目介绍 二丶服务器Server 三丶测试TCP server服务器 四丶客户端Client 五丶测试客户端向服务器下载 ...

  8. TCP文件下载器(Python)

    使用TCP编写一个文件下载器,分为客户端和服务器,此博客为上一篇的延伸,增加了文件读写编程. 下载器服务器程序: import socketdef send_file_client(new_clien ...

  9. java web批量下载_JAVAWEB批量文件下载器

    [实例简介] JAVAWEB批量文件下载器,实现文件批量下载,队列方式,一个下载完成,再下载另一个. [实例截图] [核心代码] 7a8f7e4d-75f1-489b-9ef8-b42f3b6da24 ...

  10. java-网络文件下载器

    今天是2017.5.25,下午在实验室没事干,想起来了写博客,就把我之前写的一个网络文件下载器放上来吧,程序界面风格美观友好,人性化,如选择下载位置时配置了文件位置选择框,下载过程中使用了进度条myB ...

最新文章

  1. 90后清华女校友范楚楚获ACM 2020唯一博士论文奖!出任MIT助理教授后再摘桂冠
  2. 特斯拉无人驾驶却在高速路驰骋,四名乘客喝酒唱歌开party,网友:12分应该扣给谁?...
  3. 测试晶面间距软件_【干货】高分辨TEM晶面间距的测量与标定丨DM软件
  4. c#中chart绘制曲线,柱状图等
  5. js获取节点的DOM操作
  6. mysql5.7.18压缩包下载_Windows安装MySQL5.7教程
  7. 【C++ STL学习之一】容器的共通能力和共通操作总结
  8. GET POST方法长度限制
  9. 今日恐慌与贪婪指数为94 贪婪程度有所上升
  10. 我的敏捷、需求分析、UML、软件设计电子书 - 下载(持续更新中)
  11. PTA—求整数段和(C语言)
  12. 在Openstack上创建并访问Kubernetes集群
  13. mac终端命令大全介绍
  14. 服务号idbase64_微信 unionid 获取 解密数据
  15. 面试珠玑 嵌入式C程序员经典笔试题一
  16. 视频号还是直播?2021年微信财富密码预言
  17. 在微软Word中插入代码并保持代码样式
  18. 小米手机访问电脑共享文件_小米手机不用数据线直接访问电脑上的文件的方法...
  19. visio的细的箭头
  20. 数据库排行榜|当 DB-Engines 遇见墨天轮国产数据库排行

热门文章

  1. javascript 设置返回顶部的效果
  2. 面向对象程序设计——类与对象的应用2
  3. 侠盗飞车秘籍-侠盗秘籍-飞车4飞车5密籍
  4. 自然数学-导数运算法则和洛必达法则
  5. uni-app图片加水印;小程序图片添加水印;使用canvas上传图片加水印
  6. 摄像头功能开发不再复杂,CameraX 助您轻松打造光影体验
  7. ppt_第一章_德塔自然语言图灵系统
  8. emoji表情特殊字符出错处理
  9. 数据库的脏读、不可重复读、幻读以及四种隔离级别
  10. 【python】office操作,doc转docx、ppt,pptx转pdf,pdf转txt