导读:
一.概述: 本文通过一个实例向大家介绍用Visual C#进行Internet通讯编程的一些基本知识。我们知道.Net类包含了请求/响应层、应用协议层、传输层等层次。在本程序中,我们运用了位于请求/响应层的WebRequest类以及WebClient类等来实现高抽象程度的Internet通讯服务。本程序的功能是完成网络文件的下载。
二.实现原理: 程序实现的原理比较简单,主要用到了WebClient类和FileStream类。其中WebClient类处于System.Net名字空间中,该类的主要功能是提供向URI标识的资源发送数据和从URI标识的资源接收数据的公共方法。我们利用其中的DownloadFile()方法将网络文件下载到本地。然后用FileStream类的实例对象以数据流的方式将文件数据写入本地文件。这样就完成了网络文件的下载。
三.实现步骤: 首先,打开Visual Studio.Net,新建一个Visual C# Windows应用程序的工程,不妨命名为"MyGetCar"。
接着,布置主界面。我们先往主窗体上添加如下控件:两个标签控件、两个文本框控件、一个按钮控件以及一个状态栏控件。最终的主窗体如下图所示:
完成主窗体的设计,我们接着完成代码的编写。
在理解了基本原理的基础上去完成代码的编写是相当容易。程序中我们主要用到的是WebClient类,不过在我们调用WebClient类的实例对象前,我们需要用WebRequest类的对象发出对统一资源标识符(URI)的请求。
try
{
WebRequest myre=WebRequest.Create(URLAddress);
}
catch(WebException exp)
{
MessageBox.Show(exp.Message,"Error");
}
这是一个try-catch语句,try块完成向URI的请求,catch块则捕捉可能的异常并显示异常信息。其中的URLAddress为被请求的网络主机名。
在请求成功后,我们就可以运用WebClient类的实例对象中的DownloadFile()方法实现文件的下载了。其函数原型如下:
public void DownloadFile( string address, string fileName);
其中,参数address为从中下载数据的 URI,fileName为要接收数据的本地文件的名称。
之后我们用OpenRead()方法来打开一个可读的流,该流完成从具有指定URI的资源下载数据的功能。其函数原型如下:
ublic Stream OpenRead(string address);
其中,参数address同上。
最后就是新建一个StreamReader对象从中读取文件的数据,并运用一个while循环体不断读取数据,只到读完所有的数据。
还有在使用以上方法时,你将可能需要处理以下几种异常:
WebException:下载数据时发生错误。
UriFormatException:通过组合 BaseAddress、address 和 QueryString 所构成的 URI 无效。
这部分的代码如下:(client为WebClient对象,在本类的开头处声明即可)
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类的实例对象将这些数据写入本地文件中:
FileStream fstr = new FileStream(Path,FileMode.OpenOrCreate,FileAccess.Write);
fstr.Write(mbyte,0,startmbyte);
这样,程序主体部分的代码已经完成了,不过要完成全部程序还需要一些工作。由于在程序接收网络文件数据的时候运用到了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
{
///
/// Form1 的摘要说明。
///
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();
///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
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.Drawi
本文转自
http://study.qqcf.com/web/224/24011.htm
分享到:
相关推荐
用Visual_C#实现文件下载功能
向大家介绍用Visual C#进行Internet通讯编程的一些基本知识。在本程序中,运用了位于请求/响应层的WebRequest类以及WebClient类等来实现高抽象程度的Internet通讯服务。本程序的功能是完成网络文件的下载。
C# 自制网络文件下载器,界面设计大神勿喷,将会持续更新,文件为压缩包,是包含源代码的,请大神看到代码后多多提建议,我就可以早日更新。 V 1.0 1.可实现下载网络文件 2.可视化操作 3.使用简单 4.无广告 5.下载...
功能:使用Windows Media Player播放音乐,视频,支持 WAV、MID、MP3、MPG、AVI、ASF、WMV、RM、RMVB等文件。 技术:C# + WinForm 版本:Microsoft Visual Studio 2008 上开发 实现功能: 1.选择文件播放 2.设置...
通过WinForm调用FluentFTP库,实现FTP客户端,具体可参考本人博客【C# 利用FluentFTP实现FTP上传下载功能】 开发工具:VisualStudio2022 开发语言:C# 开发框架:.Net Framework4.8及以上
实例47 计算器功能实现 实例48 小闹钟 小结 第3章 图形与图像处理 实例49 简单画图 实例50 使用OnPaint绘制图形 实例51 绘制贝赛尔曲线 实例52 显示图像 实例53 消除图片背景 实例54 ...
不过可以自己通过图形绘制,文档和范例相对比较齐全,没有太大的应用难度,再加上有宇宙最强的IDE Visual Studio的支持,功能实现和调试过程比较友好。 相比于其他的资源,所需积分较少,只要几个辛苦费积分即可,...
c#宾馆管理系统实现—功能全 下面是在简体中文Microsoft Windows Server 2003(SP1)/Microsoft Office 2003(SP2)/ Microsoft SQL Server 2005/Microsoft Visual Studio 2005环境中配置《宾馆酒店管理系统》数据库及...
本项目通过用C#进行Visio进行二次开发,实现了绘制电气接线图, 并能实现潮流计算。 0 前言 中国电力行业属于垄断行业。从目前来看,改革我国电力行业,打破垄断引入竞争已成为大势所趋,但是,电力行业与其他垄断...
Visual Studio支持Visual C#,这是通过功能齐全的代码编辑器、项目模板、设计器、代码向导、功能强大且易于使用的调试器以及其他工具实现的。通过.NET Framework类库,可以访问多种操作系统服务和其他有用的精心设计...
【工控老马出品,必属精品,质量保证,亲测能用】 资源名:欧姆龙PLC通讯程序源码(HOST LINK 实现) 资源类型:程序源代码 ... 1、开发环境为Visual Studio 2012使用.net 4.0开发 适合人群:新手及有一定经验的开发人员
用Visual C# 2005做该程序,实现将一个目标文件拷贝到目标路径下的功能,在拷贝的过程中随时可以中断。主要用到了System.IO中的流、目录和文件以及System.Threading的功能。
可以播放的所有文件,实现播放器最基本功能,添加单曲,搜索整个文件夹中所有MP3文件.显示播放列表.上下移动播放列表中的曲目,删除选定曲目. 补充说明: 这是本人学C#以来做的第二个项目,以前做了一个记事本(模仿实现...
VC++ MFC 对话框 ,编辑框 按钮 实现文件打开和复制文件功能
VS2010实现的计算器小程序,能实现简单的减加乘除,欢迎下载讨论并指正其中问题。计算器也有待完善功能。
Visual C# 2005 程序设计自学手册 *****是随书源码光盘***** *****人民邮电出版社***** **长春明日科技组织编写** 本书从初学者角度出发,通过通俗易懂的语言和大量生动典型的实例,由浅入深、循序渐进地介绍使用...
C#实现的下载工具,采用了多线程下载,可以设置线程数,同时支持暂停,断点续传的功能。 下载中会生成临时文件,最后下载完合并,对学习下载的用户很有帮助。 注意: 开发环境为Visual Studio 2010
功能:使用Windows Media Player播放音乐,视频,支持 WAV、MID、MP3、MPG、AVI、ASF、WMV、RM、RMVB等文件。 技术:C# + WinForm 版本:Microsoft Visual Studio 2008 上开发 实现功能: 1.选择文件播放 2.设置...