Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1.         private void StartDownload(string URL, string fileName)
  2.         {
  3.             Thread thread = new Thread(() =>
  4.             {                
  5.                 WebClient wc = new WebClient();
  6.                 wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ClientDownloadProgressChanged);
  7.                 wc.DownloadFileCompleted += (sender, e) => ClientDownloadFileCompleted(sender, e, fileName);
  8.                 wc.DownloadFileAsync(new Uri(URL), fileName);                
  9.             });
  10.             thread.Start();
  11.             rtBox.Text = fileName.Replace(directory, "") + " está sendo baixado";
  12.         }
  13.         void ClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  14.         {
  15.             BeginInvoke((MethodInvoker)delegate
  16.             {
  17.                 rtBox.Text += ".";
  18.                 if (rtBox.Text == "data.rar está sendo baixado.....")
  19.                     rtBox.Text = "data.rar está sendo baixado";
  20.  
  21.                 double bytesIn = double.Parse(e.BytesReceived.ToString());
  22.                 double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
  23.                 double percentage = bytesIn / totalBytes * 100;
  24.                 label2.Text = "Baixado " + (e.BytesReceived) / 1024 + " KBs de " + (e.TotalBytesToReceive) / 1024 + " KBs";
  25.                 progressBar.Value = int.Parse(Math.Truncate(percentage).ToString());
  26.             });
  27.         }
  28.         void ClientDownloadFileCompleted(object sender, AsyncCompletedEventArgs e, string fileName)
  29.         {
  30.             BeginInvoke((MethodInvoker)delegate
  31.             {
  32.                 rtBox.Text = string.Empty;
  33.                 rtBox.Text += "Arquivo " + fileName.Replace(directory, "") + " baixado\n";
  34.  
  35.                 if (fileName.Replace(directory, "") == "data.rar")
  36.                 {
  37.                     label2.Text = "Atualização completa!";
  38.                     UnRAR(fileName);
  39.                     LineChanger(server_version, "pid.txt", 4);              
  40.                     Process.Start(File.ReadLines("pid.txt").Skip(5).Take(1).First());
  41.                     Application.Exit();
  42.                 }                    
  43.             });
  44.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement