2、代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace WebClientUpload
{
public partial class Form3 : Form
{
WebClient c;
public Form3()
{
InitializeComponent();
c = new System.Net.WebClient();
c.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(c_DownloadProgressChanged);
c.DownloadFileCompleted += new AsyncCompletedEventHandler(c_DownloadFileCompleted);
c.Proxy = WebRequest.DefaultWebProxy;
//c.Proxy.Credentials = new NetworkCredential("admin", "admin_password", "domain");
}
void c_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("ok");
this.Close();
}
private void btnDownload_Click(object sender, EventArgs e)
{
}
void c_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
this.label1.Text = "已完成:" + progressBar1.Value.ToString()+"%";
}
private void btnQuit_Click(object sender, EventArgs e)
{
}
private void Form3_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//上传文件
c.DownloadFileAsync(new Uri("d:\\download\\阿甘正传.rm"), @"e:\11.rm");
}
private void button2_Click(object sender, EventArgs e)
{
//取消上传
if (c != null)
{
c.CancelAsync();
}
Close();
}
}
}
附:另种进度条控制方案:
Form1调用 Form2(进度条窗体)
Form2 pj = new Form2();
pj.Show();
//开始处理大量耗时工作
string sor = "";
int j = 100000;
for (int i = 0; i < j; i++)
{
sor += i.ToString();
if (i % (j / 100) == 0)
{
pj.progressBar1.Value++;
pj.label2.Text = "已完成:" + string.Format("{0:p}", (double)pj.progressBar1.Value / 100);
Application.DoEvents();
}
}
//工作完成,关闭进度条窗体
pj.Close();
分享到:
相关推荐
WebClient下载文件展示进度条
C# WebClient 上传文件 WebClient上传文件至服务器
C# WEBClient 文件上传下载
本篇文章主要介绍了C#中WebClient实现文件下载,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
C#的Winform开发的下载器,下载文件夹,带速度计算和文件下载计算。
本代码通过webclient下载,下载文件支持回调显示进度等。
本程序就主要是通过C# 中的WebClient 类实现对文件的下载功能,其中涉及到进度条,对下载成功与下载失败都会给出相应的提示,本程序是通过VS 2005 开发的,希望给需要在项目中用到下载这个功能的人有所帮助!...
该资源使用Webclient 类后台自动获取网络上指定的文件内容,下载过程中可以显示实时下载速度和进度,下载完成之后又提示信息。
C#下载文件 显示下载进度及速度 /// 开启界面刷新计时器 (不使用线程,防止阻塞造成界面刷新不及时) timer1.Enabled = true; try { WebRequest httpRequest = WebRequest.Create(tbFile.Text.Trim()); ...
与webclient下载功能类似,3个新增的可缺省参数: Prog参数代表progressbar,Value代表lable,Cover代表是否替换同名文件。
下载开箱即用:C#使用WebClient获取网页源文件例子:该例子演示了通过WebClient获取百度首页源文件,并进行UTF8解码,存入一个字符串中。
WebClient上传下载源码 我们先在IIS网站中建立一个文件夹,此处为"Mp3",并设置此文件夹相关读写权限。 例1:使用WebClient中的UploadFile方法上传文件。代码如下。使用此方法需要将上传的文件夹权限设置为 IIS来宾...
C#中使用WebClient下载指定url的网络照片示例源码--VS2013
使用WebClient实现文件下载,进度条反应下载进度。
使用WebClient类和FileStream类实现文件下载
C#HTTP方式下载,含断点续传,限制速度
//用于新手学习异步操作 //例子简单,下载字符串也是一个道理
使用WebClient的UploadFileAsync、UploadFile、DownloadFile、DownloadFileAsync方法,异步调用服务器上传下载数据。
C#中的WebClient类编写整站下载软件
C#下载ftp指定目录下所有文件。ftp地址、目录名、保存路径都写入了代码中,用户可根据需要自己修改即可。可下载目录下所有文件和文件夹。注解较少,我也是参照网上写的,本机运行正常。