Advertisement
Guest User

Untitled

a guest
Mar 14th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1. sing 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.Windows.Forms;
  9. using System.Net;
  10. using System.IO;
  11. using System.Threading;
  12.  
  13. namespace Letoltesvezerlo
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         private Thread thrLetolt;
  18.         private Stream strValasz;
  19.         private Stream strHelyiMentes;
  20.         private HttpWebRequest webKerelem;
  21.         private HttpWebResponse webValasz;
  22.         private static int LetoltesSzazalek;
  23.         private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);
  24.  
  25.         public Form1()
  26.         {
  27.             InitializeComponent();
  28.         }
  29.  
  30.         private void btnLetoltes_Click(object sender, EventArgs e)
  31.         {
  32.  
  33.             lblFolyamat.Text = "Letöltés elkezdve!";
  34.             thrLetolt = new Thread(new ThreadStart(Letolt));
  35.             thrLetolt.Start();
  36.         }
  37.  
  38.         private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
  39.         {
  40.  
  41.             LetoltesSzazalek = Convert.ToInt32((BytesRead * 100) / TotalBytes);
  42.             prgLetolt.Value = LetoltesSzazalek;
  43.             lblFolyamat.Text = "ennyi van letöltve " + BytesRead + " -ből " + TotalBytes + " (" + LetoltesSzazalek + "%)";
  44.         }
  45.  
  46.         private void Letolt()
  47.         {
  48.             using (WebClient wcDownload = new WebClient())
  49.             {
  50.                 try
  51.                 {
  52.  
  53.                     webKerelem = (HttpWebRequest)WebRequest.Create(textBoxUrl.Text);
  54.                     webKerelem.Credentials = CredentialCache.DefaultCredentials;
  55.                     webValasz = (HttpWebResponse)webKerelem.GetResponse();
  56.  
  57.                     Int64 fileSize = webValasz.ContentLength;
  58.                     strValasz = wcDownload.OpenRead(textBoxUrl.Text);
  59.                     strHelyiMentes = new FileStream(textBoxUtvonal.Text, FileMode.Create, FileAccess.Write, FileShare.None);
  60.  
  61.                     int bytesSize = 0;
  62.                     byte[] downBuffer = new byte[2048];
  63.  
  64.                     while ((bytesSize = strValasz.Read(downBuffer, 0, downBuffer.Length)) > 0)
  65.                     {
  66.                         strHelyiMentes.Write(downBuffer, 0, bytesSize);
  67.  
  68.                         this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strHelyiMentes.Length, fileSize });
  69.                     }
  70.                 }
  71.                 finally
  72.                 {
  73.  
  74.                     strValasz.Close();
  75.                     strHelyiMentes.Close();
  76.                 }
  77.             }
  78.         }
  79.  
  80.         private void btnAllj_Click(object sender, EventArgs e)
  81.         {
  82.  
  83.             webValasz.Close();
  84.             strValasz.Close();
  85.             strHelyiMentes.Close();
  86.  
  87.             thrLetolt.Abort();
  88.  
  89.             prgLetolt.Value = 0;
  90.             lblFolyamat.Text = "Letöltés megállítva!";
  91.         }
  92.  
  93.         private void Form1_Load(object sender, EventArgs e)
  94.         {
  95.  
  96.         }
  97.  
  98.         private void label1_Click(object sender, EventArgs e)
  99.         {
  100.  
  101.         }
  102.     }
  103.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement