以下代码 : 主窗体
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;
using System.IO;
using System.Threading;
namespace DownLoadHttp
{
public partial class Form1 : Form
{
public Form1()
{
//防止线程间操作无效
//如果不写,会产生:
//线程间操作无效: 从不是创建控件“btnDown”的线程访问它。
//因为控件的创建在另外一个线程。
Control.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
this.txtSrcAddress.Text = @"http://down.360safe.com/inst.exe";
this.txtTarAddress.Text = @"D:";
}
private void btnDown_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(this.StartDownLoad));
th.Start();
}
private void StartDownLoad()
{
this.btnDown.Enabled = false;
this.statusBar.Text = "正在下载...";
//要下载的文件http地址
//例如:(http://www.YourSiteAddress.com/SoftWare/Soft1.rar)
string url = this.txtSrcAddress.Text;
//截取文件名称
int n = url.LastIndexOf('/');
string fileName = url.Substring(n + 1, url.Length - n - 1);
//保存的路径+\文件名
string dir = this.txtTarAddress.Text;
string filepath = dir+"\\"+fileName;
HttpDownLoad httpDownLoad = new HttpDownLoad();
httpDownLoad.httpDownFile(url,filepath, this.toolStripProgressBar, this.toolStripStatusLabel);
this.btnDown.Enabled = true;
}
}
}
以下代码 : 下载方法
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Web;
using System.Windows.Forms;
namespace DownLoadHttp
{
/// <summary>
/// httpDownLoad 的摘要说明。
/// </summary>
public class HttpDownLoad
{
private long fileLength;
//已经下载文件大小
private long downLength;
private static bool stopDown;
//构造函数
public HttpDownLoad()
{
fileLength = 0;
downLength = 0;
stopDown = false;
}
/// <summary>
/// 文件下载
/// </summary>
/// <param name="url">连接</param>
/// <param name="fileName">本地保存文件名</param>
/// <param name="progressBar">进度条</param>
public void httpDownFile(string url, string fileName, ToolStripProgressBar progressBar)
{
ToolStripLabel lable = new ToolStripLabel();
httpDownFile(url, fileName, progressBar, lable);
lable.Dispose();
}
/// <summary>
/// 文件下载
/// </summary>
/// <param name="url">连接</param>
/// <param name="fileName">本地保存文件名</param>
/// <param name="progressBar">进度条</param>
/// <param name="label">返回已经下载的百分比</param>
public void httpDownFile(string url, string fileName, ToolStripProgressBar progressBar, ToolStripLabel lable)
{
stopDown = false;
Stream str = null, fs = null;
try
{
//获取下载文件长度
fileLength = getDownLength(url);
downLength = 0;
if (fileLength > 0)
{
WebClient DownFile = new WebClient();
str = DownFile.OpenRead(url);
//判断并建立文件
if (createFile(fileName))
{
byte[] mbyte = new byte[1024];
int readL = str.Read(mbyte, 0, 1024);
fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
//读取流
while (readL != 0)
{
if (stopDown)
break;
downLength += readL;//已经下载大小
fs.Write(mbyte, 0, readL);//写文件
readL = str.Read(mbyte, 0, 1024);//读流
progressBar.Value = (int)(downLength * 100 / fileLength);
lable.Text = "已经下载" + progressBar.Value.ToString() + "%";
System.Windows.Forms.Application.DoEvents();
}
str.Close();
fs.Close();
lable.Text = "下载已经完成";
}
}
}
catch (Exception ex)
{
if (str != null)
str.Close();
if (fs != null)
fs.Close();
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 文件下载
/// </summary>
/// <param name="url">连接</param>
/// <param name="fileName">本地保存文件名</param>
public void httpDownFile(string url, string fileName)
{
try
{
WebClient DownFile = new WebClient();
DownFile.DownloadFile(url, fileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 获取下载文件大小
/// </summary>
/// <param name="url">连接</param>
/// <returns>文件长度</returns>
private long getDownLength(string url)
{
try
{
WebRequest wrq = WebRequest.Create(url);
WebResponse wrp = (WebResponse)wrq.GetResponse();
wrp.Close();
return wrp.ContentLength;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return 0;
}
}
/// <summary>
/// 建立文件(文件如已经存在,删除重建)
/// </summary>
/// <param name="fileName">文件全名(包括保存目录)</param>
/// <returns></returns>
private bool createFile(string fileName)
{
try
{
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Stream s = File.Create(fileName);
s.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
public void downClose()
{
stopDown = true;
}
}
}