Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Net;
- using System.Windows;
- using System.ComponentModel;
- using System.IO;
- namespace Download_Manager
- {
- class DownloadFiles
- {
- public List<Uri> downloadQueue = new List<Uri>();
- //Index of file in queue to download
- int fileToDownloadIndex;
- //download percentage
- int percent;
- //file size
- int totalBytesToRecieve;
- bool isDownloadComplete;
- string downloadPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Files";
- public int FileToDownloadIndex
- {
- get { return fileToDownloadIndex; }
- set { fileToDownloadIndex = value; }
- }
- public int Percent
- {
- get { return percent; }
- set { percent = value; }
- }
- public int TotalBytesToRecieve
- {
- get { return totalBytesToRecieve; }
- set { totalBytesToRecieve = value; }
- }
- public bool IsDownloadComplete
- {
- get { return isDownloadComplete; }
- set { isDownloadComplete = value; }
- }
- private string getContentType(Uri url)
- {
- string contentType = "";
- try
- {
- var request = HttpWebRequest.Create(url);
- using (var response = request.GetResponse())
- {
- contentType = response.ContentType;
- }
- }
- catch (WebException)
- {
- MessageBox.Show("Error fetching file extension");
- }
- return contentType;
- }
- private void downloadFile(Uri url)
- {
- //if (string.IsNullOrEmpty(contentType))
- //{
- //MessageBox.Show("Error fetching file extension. Would you like to manually set it?");
- //TO DO
- //}
- //else
- {
- if (!Directory.Exists(downloadPath))
- {
- Directory.CreateDirectory(downloadPath);
- }
- try
- {
- using (WebClient wb = new WebClient())
- {
- wb.DownloadProgressChanged += new DownloadProgressChangedEventHandler(progressChanged);
- wb.DownloadFileCompleted += downloadCompleted;
- string contentType = getContentType(url);
- string fileExtension = contentType.Substring(contentType.IndexOf('/') + 1);
- wb.DownloadFileAsync(url, downloadPath + @"\" + fileToDownloadIndex.ToString() + "." + fileExtension);
- //wb.DownloadFileAsync(url, downloadPath + @"\" + fileToDownloadIndex.ToString() + "." + "zip");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- private void progressChanged(object sender, DownloadProgressChangedEventArgs e)
- {
- //download percentage
- percent = Convert.ToInt32(e.BytesReceived);
- //size of file
- totalBytesToRecieve = Convert.ToInt32(e.TotalBytesToReceive);
- }
- private void downloadCompleted(object sender, EventArgs e)
- {
- //Number of urls in Queue
- int urlCount = downloadQueue.Count - 1;
- if (fileToDownloadIndex != urlCount)
- {
- fileToDownloadIndex++;
- downloadFile(downloadQueue[fileToDownloadIndex]);
- }
- else
- {
- isDownloadComplete = true;
- percent = 0;
- totalBytesToRecieve = 0;
- fileToDownloadIndex = 0;
- downloadQueue.Clear();
- }
- }
- public void startDownload()
- {
- downloadFile(downloadQueue[FileToDownloadIndex]);
- }
- }
- }
Add Comment
Please, Sign In to add comment