網絡下載器

          網絡下載器

          最近做了一個網絡下載器,拿出來曬一下。希望有人用得着

           界面如下:

           

           核心代碼如下:

               

           下載速度的計算:

 

         

          下載完成:

        

         代碼實現:

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");
        }

        
    }
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章