C# - Simple Downloader


SUBMITTED BY: Guest

DATE: Nov. 30, 2014, 4:13 p.m.

FORMAT: C#

SIZE: 6.2 kB

HITS: 6507

  1. using System;
  2. using System.Threading;
  3. using System.IO;
  4. using System.Net;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using System.Runtime.InteropServices;
  13. namespace EasyDownloader
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. private void button2_Click(object sender, EventArgs e)
  22. {
  23. SaveFileDialog SFD = new SaveFileDialog();
  24. SFD.Filter = "All files (*.*)|(*.*)";
  25. SFD.FilterIndex = 1;
  26. SFD.RestoreDirectory = true;
  27. SFD.ShowDialog();
  28. if (SFD.FileName != String.Empty)
  29. {
  30. textBox2.Text = SFD.FileName;
  31. }
  32. }
  33. private void button1_Click(object sender, EventArgs e)
  34. {
  35. try
  36. {
  37. if (textBox1.Text == String.Empty)
  38. {
  39. throw new RemoteFileEmptyException("Pole URL nie może być puste.");
  40. }
  41. if (textBox2.Text == String.Empty)
  42. {
  43. throw new DestinationFileEmptyException("Pole z plikiem docelowym nie może być puste.");
  44. }
  45. textBox1.ReadOnly = true;
  46. string URL = String.Empty;
  47. string DestFile = String.Empty;
  48. URL = textBox1.Text;
  49. DestFile = textBox2.Text;
  50. Console.ForegroundColor = ConsoleColor.White;
  51. Console.WriteLine("URL = " + URL);
  52. Console.WriteLine("DestFile = " + DestFile);
  53. Console.ForegroundColor = ConsoleColor.Red;
  54. getFile(URL, DestFile);
  55. textBox1.ReadOnly = false;
  56. }
  57. catch (RemoteFileEmptyException ex)
  58. {
  59. MessageBox.Show("Pole URL nie może być puste.", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
  60. Console.WriteLine(ex);
  61. }
  62. catch (DestinationFileEmptyException ex)
  63. {
  64. MessageBox.Show("Pole z plikiem docelowym nie może być puste.", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
  65. Console.WriteLine(ex);
  66. }
  67. catch (Exception ex)
  68. {
  69. Console.WriteLine(ex);
  70. }
  71. }
  72. private void Form1_Load(object sender, EventArgs e)
  73. {
  74. DialogResult DR = MessageBox.Show("Czy chcesz uruchomić konsolę debugującą?", "Konsola", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  75. if (DR == DialogResult.Yes)
  76. {
  77. AllocConsole();
  78. }
  79. Console.ForegroundColor = ConsoleColor.Red;
  80. }
  81. private void getFile(string URL, string DestFile)
  82. {
  83. WebClient WC = new WebClient();
  84. Uri uri = new Uri(URL);
  85. try
  86. {
  87. WC.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
  88. WC.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
  89. WC.DownloadFileAsync(uri, DestFile);
  90. button1.Enabled = false;
  91. }
  92. catch (WebException ex)
  93. {
  94. 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);
  95. Console.WriteLine(ex);
  96. }
  97. catch (Exception ex)
  98. {
  99. Console.WriteLine(ex);
  100. }
  101. }
  102. void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  103. {
  104. double bytesIn = double.Parse(e.BytesReceived.ToString());
  105. double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
  106. double percentage = bytesIn / totalBytes * 100;
  107. progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
  108. label3.Text = bytesIn + "/" + totalBytes + " - " + Math.Round(percentage) + "% ukończono.";
  109. }
  110. void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
  111. {
  112. Console.ForegroundColor = ConsoleColor.Green;
  113. Console.WriteLine("Pobieranie zakończone");
  114. Console.ForegroundColor = ConsoleColor.Red;
  115. button1.Enabled = true;
  116. label3.Text = String.Empty;
  117. progressBar1.Value = 0;
  118. MessageBox.Show("Pobieranie ukończone!", "Zakończono", MessageBoxButtons.OK, MessageBoxIcon.Information);
  119. }
  120. [DllImport("kernel32.dll")]
  121. static extern bool AllocConsole();
  122. }
  123. public class DestinationFileEmptyException : Exception
  124. {
  125. public DestinationFileEmptyException()
  126. {
  127. }
  128. public DestinationFileEmptyException(string message) : base(message)
  129. {
  130. }
  131. public DestinationFileEmptyException(string message, Exception inner) : base(message, inner)
  132. {
  133. }
  134. }
  135. public class RemoteFileEmptyException : Exception
  136. {
  137. public RemoteFileEmptyException()
  138. {
  139. }
  140. public RemoteFileEmptyException(string message) : base(message)
  141. {
  142. }
  143. public RemoteFileEmptyException(string message, Exception inner) : base(message, inner)
  144. {
  145. }
  146. }
  147. }

comments powered by Disqus