网络下载器
最近做了一个网络下载器,拿出来晒一下。希望有人用得着
界面如下:
核心代码如下:
下载速度的计算:
下载完成:
代码实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Downloader
{
public partial class FrmDownload : Form
{
int intX = 0; //记录鼠标点击时X的坐标
int intY = 0; //记录鼠标点击时Y的坐标
Thread thSpleedOrPencent = null; //建立记录下载速度的线程
int ttt = 0; ///用来保存每秒下载的数据大小
int count = 0; //用来保存
long intReadLength = 0;
int int_ = 0; //文件的总大小
string[] str = new string[5];
public FrmDownload()
{
InitializeComponent();
}
#region 自定义方法
/// <summary>
/// 委托
/// </summary>
/// <param name="str"></param>
private delegate void ShowDelegate(string[] str);
/// <summary>
/// 控件的显示
/// </summary>
/// <param name="str"></param>
private void Show(string[] str)
{
if (str != null && str.Length == 5)
{
if (this.InvokeRequired)
{
ShowDelegate sd = Show;
Invoke(sd,new object[]{str});
}
else
{
progressBar_.Value = int.Parse(str[0]);
lblPencent.Text = str[0] + "%";
lblDownloadSpeed.Text = str[3];
lblExcessTime.Text = str[4];
lblShowExcess.Text = str[1] + "kb / " + str[2] + "kb";
if (progressBar_.Value == 100)
txtLinks.ReadOnly = false;
}
}
}
/// <summary>
/// 时针的回调函数
/// </summary>
/// <param name="obj"></param>
private void Timer(object obj)
{
double d = (ttt / 1024.00);
str[3] = d.ToString("#.00") + "kb/s"; //保存下载速度
str[4] = (((intReadLength - int_) / 1024.00) / d).ToString("#."); //保存剩余时间
ttt = 0;
}
/// <summary>
/// 种子下载
/// </summary>
private void SpleedOrPencent()
{
if (txtLinks.Text.Trim() == "") return;
try
{
WebRequest wr = WebRequest.Create(txtLinks.Text.Trim());//客户端对服务器端发送请求
WebResponse wrp = wr.GetResponse();//服务器端对客户端做出响应
long length = wrp.ContentLength;
if (length > 0)
{
string[] strSplit = txtLinks.Text.Trim().Split('/');
string strFile = strSplit[strSplit.Length - 1].ToString();//获取文件名
byte[] b = new byte[length];
Stream s = wrp.GetResponseStream();
System.Threading.Timer t = new System.Threading.Timer(Timer, null, 0, 1000);//每隔一段时间记录已下载的数据。从而计算下载速度
intReadLength = length;
while ((count = s.Read(b, 0, 5000)) != 0)
{
if (str[0] == "100") break;
int_ += count;
ttt += count;
str[0] = Convert.ToInt32((int_ / (length * 1.00)) * 100).ToString(); //保存下载的百分比
str[1] = (int_ / 1024.00).ToString("#.00"); //保存已下载的数据大小
str[2] = (b.Length / 1024.00).ToString("#.00"); //保存文件的总大小
Show(str);
Application.DoEvents();
}
s.Flush();
s.Close();
if (progressBar_.Value == progressBar_.Maximum)
{
MessageBox.Show("下载完成");
}
}
}
catch (FormatException) //捕获格式异常
{
MessageBox.Show("你输入的地址有误!");
}
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
int_ = 0;
thSpleedOrPencent =new Thread (SpleedOrPencent);
thSpleedOrPencent.Start();
txtLinks.ReadOnly = true;
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
intX = e.X;
intY = e.Y;
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Left = Left + e.X - intX;
Top = Top + e.Y - intY;
}
}
private void btnMix_MouseEnter(object sender, EventArgs e)
{
btnMix.BackgroundImage = Image.FromFile(Application.StartupPath+"/Image/1.png");
}
private void btnMix_MouseLeave(object sender, EventArgs e)
{
btnMix.BackgroundImage = Image.FromFile(Application.StartupPath + "/Image/mix.png");
}
private void btnClose_MouseEnter(object sender, EventArgs e)
{
btnClose.BackgroundImage = Image.FromFile(Application.StartupPath + "/Image/0.png");
}
private void btnClose_MouseLeave(object sender, EventArgs e)
{
btnClose.BackgroundImage = Image.FromFile(Application.StartupPath + "/Image/close.png");
}
private void btnClose_Click(object sender, EventArgs e)
{
string[] strPoint = lblPencent.Text.Split('%');
if (int.Parse(strPoint[0]) != 100 && thSpleedOrPencent != null)
{
DialogResult dr = MessageBox.Show("文件还未下完,是否取消下载?","提示" , MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
thSpleedOrPencent.Abort();
this.Close();
}
}
else
{
thSpleedOrPencent.Abort();
this.Close();
}
}
private void btnMix_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void txtLinks_MouseDoubleClick(object sender, MouseEventArgs e)
{
MessageBox.Show("fe");
}
}
}