using System; using System.Threading; using System.IO; using System.Net; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace EasyDownloader { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { SaveFileDialog SFD = new SaveFileDialog(); SFD.Filter = "All files (*.*)|(*.*)"; SFD.FilterIndex = 1; SFD.RestoreDirectory = true; SFD.ShowDialog(); if (SFD.FileName != String.Empty) { textBox2.Text = SFD.FileName; } } private void button1_Click(object sender, EventArgs e) { try { if (textBox1.Text == String.Empty) { throw new RemoteFileEmptyException("Pole URL nie może być puste."); } if (textBox2.Text == String.Empty) { throw new DestinationFileEmptyException("Pole z plikiem docelowym nie może być puste."); } textBox1.ReadOnly = true; string URL = String.Empty; string DestFile = String.Empty; URL = textBox1.Text; DestFile = textBox2.Text; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("URL = " + URL); Console.WriteLine("DestFile = " + DestFile); Console.ForegroundColor = ConsoleColor.Red; getFile(URL, DestFile); textBox1.ReadOnly = false; } catch (RemoteFileEmptyException ex) { MessageBox.Show("Pole URL nie może być puste.", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error); Console.WriteLine(ex); } catch (DestinationFileEmptyException ex) { MessageBox.Show("Pole z plikiem docelowym nie może być puste.", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error); Console.WriteLine(ex); } catch (Exception ex) { Console.WriteLine(ex); } } private void Form1_Load(object sender, EventArgs e) { DialogResult DR = MessageBox.Show("Czy chcesz uruchomić konsolę debugującą?", "Konsola", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (DR == DialogResult.Yes) { AllocConsole(); } Console.ForegroundColor = ConsoleColor.Red; } private void getFile(string URL, string DestFile) { WebClient WC = new WebClient(); Uri uri = new Uri(URL); try { WC.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); WC.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); WC.DownloadFileAsync(uri, DestFile); button1.Enabled = false; } catch (WebException ex) { MessageBox.Show("Wystąpił błąd podczas pobierania.\nPowodem może być\n- niepoprawny URL\n- niepoprawny plik docelowy\n- plik nie istnieje na serwerze", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error); Console.WriteLine(ex); } catch (Exception ex) { Console.WriteLine(ex); } } void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { double bytesIn = double.Parse(e.BytesReceived.ToString()); double totalBytes = double.Parse(e.TotalBytesToReceive.ToString()); double percentage = bytesIn / totalBytes * 100; progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString()); label3.Text = bytesIn + "/" + totalBytes + " - " + Math.Round(percentage) + "% ukończono."; } void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Pobieranie zakończone"); Console.ForegroundColor = ConsoleColor.Red; button1.Enabled = true; label3.Text = String.Empty; progressBar1.Value = 0; MessageBox.Show("Pobieranie ukończone!", "Zakończono", MessageBoxButtons.OK, MessageBoxIcon.Information); } [DllImport("kernel32.dll")] static extern bool AllocConsole(); } public class DestinationFileEmptyException : Exception { public DestinationFileEmptyException() { } public DestinationFileEmptyException(string message) : base(message) { } public DestinationFileEmptyException(string message, Exception inner) : base(message, inner) { } } public class RemoteFileEmptyException : Exception { public RemoteFileEmptyException() { } public RemoteFileEmptyException(string message) : base(message) { } public RemoteFileEmptyException(string message, Exception inner) : base(message, inner) { } } }