[C#] 下载 →→→→→进入此内容的聊天室

来自 , 2020-06-08, 写在 C#, 查看 131 次.
URL http://www.code666.cn/view/e113bb92
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Diagnostics;
  12.  
  13. namespace MyDownload
  14. {
  15.     public partial class MyDownload : Form
  16.     {
  17.         string Url = "";        //要下载的文件的地址
  18.         string filePath = "";   //下载后文件保存地址
  19.         //private const string applicationFile = "LMS";
  20.  
  21.         /// <summary>
  22.         /// 构造函数
  23.         /// </summary>
  24.         /// <param name="Url"></param>
  25.         /// <param name="filePath"></param>
  26.         public MyDownload(string Url, string filePath)
  27.         {
  28.             InitializeComponent();
  29.             this.label1.Text = "";
  30.             this.Url = Url;
  31.             this.filePath = filePath;
  32.         }
  33.  
  34.         /// <summary>
  35.         /// 清楚已有文件
  36.         /// </summary>
  37.         /// <param name="filepath"></param>
  38.         private void RemoveSetupFile(string filepath)
  39.         {
  40.             try
  41.             {
  42.                 string folder = new DirectoryInfo(filepath).FullName;
  43.                 //if (File.Exists(folder + @"\" + applicationFile + ".exe"))
  44.                 //{
  45.                 //    File.Delete(folder + @"\" + applicationFile + ".exe");
  46.                 //}
  47.                 //if (File.Exists(folder + @"\" + applicationFile + ".msi"))
  48.                 //{
  49.                 //    File.Delete(folder + @"\" + applicationFile + ".msi");
  50.                 //}
  51.                 if(File.Exists(folder))
  52.                 {
  53.                     File.Delete(folder);
  54.                 }
  55.             }
  56.             catch { }
  57.         }
  58.  
  59.         /// <summary>
  60.         /// 下载
  61.         /// </summary>
  62.         /// <param name="sender"></param>
  63.         /// <param name="e"></param>
  64.         private void btnDown_Click(object sender, EventArgs e)
  65.         {
  66.             RemoveSetupFile(filePath);
  67.             DownloadFile(Url, filePath);
  68.             Process.Start(filePath);
  69.             Application.Exit();
  70.         }
  71.  
  72.         /// <summary>        
  73.         /// c#,.net 下载文件        
  74.         /// </summary>        
  75.         /// <param name="URL">下载文件地址</param>
  76.         /// <param name="Filename">下载后的存放地址</param>
  77.         /// <param name="Prog">用于显示的进度条</param>
  78.         public void DownloadFile(string URL, string filename)
  79.         {
  80.             float percent = 0;
  81.             try
  82.             {
  83.                 System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
  84.                 System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
  85.                 long totalBytes = myrp.ContentLength;
  86.                 if (prog != null)
  87.                 {
  88.                     prog.Maximum = (int)totalBytes;
  89.                 }
  90.                 System.IO.Stream st = myrp.GetResponseStream();
  91.                 System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
  92.                 long totalDownloadedByte = 0;
  93.                 byte[] by = new byte[1024];
  94.                 int osize = st.Read(by, 0, (int)by.Length);
  95.                 while (osize > 0)
  96.                 {
  97.                     totalDownloadedByte = osize + totalDownloadedByte;
  98.                     System.Windows.Forms.Application.DoEvents();
  99.                     so.Write(by, 0, osize);
  100.                     if (prog != null)
  101.                     {
  102.                         prog.Value = (int)totalDownloadedByte;
  103.                     }
  104.                     osize = st.Read(by, 0, (int)by.Length);
  105.  
  106.                     percent = (float)totalDownloadedByte / (float)totalBytes * 100;
  107.                     label1.Text = "当前下载进度" + percent.ToString() + "%";
  108.                     System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
  109.                 }
  110.                 so.Close();
  111.                 st.Close();
  112.             }
  113.             catch (System.Exception)
  114.             {
  115.                 throw;
  116.             }
  117.             this.Close();
  118.         }
  119.  
  120.         /// <summary>
  121.         /// 取消
  122.         /// </summary>
  123.         /// <param name="sender"></param>
  124.         /// <param name="e"></param>
  125.         private void btn_Cancel_Click(object sender, EventArgs e)
  126.         {
  127.             Process.Start(filePath);
  128.             Application.Exit();
  129.         }
  130.     }
  131. }
  132.  

回复 "下载"

这儿你可以回复上面这条便签

captcha