Guest User

Download File Class

a guest
Apr 9th, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7. using System.Net;
  8. using System.Windows;
  9. using System.ComponentModel;
  10. using System.IO;
  11.  
  12.  
  13. namespace Download_Manager
  14. {
  15.     class DownloadFiles
  16.     {
  17.         public List<Uri> downloadQueue = new List<Uri>();
  18.  
  19.         //Index of file in queue to download
  20.         int fileToDownloadIndex;
  21.  
  22.         //download percentage
  23.         int percent;
  24.         //file size
  25.         int totalBytesToRecieve;
  26.  
  27.         bool isDownloadComplete;
  28.  
  29.         string downloadPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Files";
  30.  
  31.         public int FileToDownloadIndex
  32.         {
  33.             get { return fileToDownloadIndex; }
  34.             set { fileToDownloadIndex = value; }
  35.         }
  36.         public int Percent
  37.         {
  38.             get { return percent; }
  39.             set { percent = value; }
  40.         }
  41.         public int TotalBytesToRecieve
  42.         {
  43.             get { return totalBytesToRecieve; }
  44.             set { totalBytesToRecieve = value; }
  45.         }
  46.         public bool IsDownloadComplete
  47.         {
  48.             get { return isDownloadComplete; }
  49.             set { isDownloadComplete = value; }
  50.         }
  51.  
  52.         private string getContentType(Uri url)
  53.         {
  54.             string contentType = "";
  55.  
  56.             try
  57.             {
  58.                 var request = HttpWebRequest.Create(url);
  59.                
  60.                 using (var response = request.GetResponse())
  61.                 {
  62.  
  63.                     contentType = response.ContentType;
  64.                 }
  65.                
  66.             }
  67.             catch (WebException)
  68.             {
  69.                 MessageBox.Show("Error fetching file extension");
  70.  
  71.             }
  72.  
  73.             return contentType;
  74.         }
  75.  
  76.  
  77.         private void downloadFile(Uri url)
  78.         {
  79.  
  80.            
  81.  
  82.             //if (string.IsNullOrEmpty(contentType))
  83.             //{
  84.                 //MessageBox.Show("Error fetching file extension. Would you like to manually set it?");
  85.                 //TO DO
  86.             //}
  87.             //else
  88.             {
  89.                 if (!Directory.Exists(downloadPath))
  90.                 {
  91.                     Directory.CreateDirectory(downloadPath);
  92.                 }
  93.  
  94.                 try
  95.                 {
  96.                     using (WebClient wb = new WebClient())
  97.                     {
  98.  
  99.                         wb.DownloadProgressChanged += new DownloadProgressChangedEventHandler(progressChanged);
  100.                         wb.DownloadFileCompleted += downloadCompleted;
  101.                         string contentType = getContentType(url);
  102.                         string fileExtension = contentType.Substring(contentType.IndexOf('/') + 1);
  103.                         wb.DownloadFileAsync(url, downloadPath + @"\" + fileToDownloadIndex.ToString() + "." + fileExtension);
  104.                         //wb.DownloadFileAsync(url, downloadPath + @"\" + fileToDownloadIndex.ToString() + "." + "zip");
  105.  
  106.                     }
  107.                 }
  108.  
  109.                 catch (Exception ex)
  110.                 {
  111.                     MessageBox.Show(ex.Message);
  112.                 }
  113.             }
  114.         }
  115.         private void progressChanged(object sender, DownloadProgressChangedEventArgs e)
  116.         {
  117.             //download percentage
  118.             percent = Convert.ToInt32(e.BytesReceived);
  119.             //size of file
  120.             totalBytesToRecieve = Convert.ToInt32(e.TotalBytesToReceive);
  121.         }
  122.         private void downloadCompleted(object sender, EventArgs e)
  123.         {
  124.             //Number of urls in Queue
  125.             int urlCount = downloadQueue.Count - 1;
  126.  
  127.             if (fileToDownloadIndex != urlCount)
  128.             {
  129.                 fileToDownloadIndex++;
  130.                 downloadFile(downloadQueue[fileToDownloadIndex]);
  131.  
  132.             }
  133.             else
  134.             {
  135.                 isDownloadComplete = true;
  136.                 percent = 0;
  137.                 totalBytesToRecieve = 0;
  138.                 fileToDownloadIndex = 0;
  139.                 downloadQueue.Clear();
  140.             }
  141.         }
  142.  
  143.         public void startDownload()
  144.         {
  145.             downloadFile(downloadQueue[FileToDownloadIndex]);
  146.         }
  147.  
  148.     }
  149. }
Add Comment
Please, Sign In to add comment