legendxs

Exception Handling - Exercise 4

Aug 12th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. /*
  4.     4. Write a program that downloads a file from Internet (e.g. http://www.devbg.org/img/Logo-BASD.jpg)
  5.     and stores it the current directory. Find in Google how to download files in C#.
  6.     Be sure to catch all exceptions and to free any used resources in the finally block.
  7. */
  8. public class DownloadAFile
  9. {
  10.     public static void DownloadFile(WebClient client)
  11.     {
  12.         client.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", @"F:\Desktop\Logo-BASD.jpg");
  13.     }
  14.  
  15.     public static void Main()
  16.     {
  17.         WebClient client = new WebClient();
  18.  
  19.         try
  20.         {
  21.             DownloadFile(client);
  22.             Console.WriteLine("File downloaded successfully.");
  23.         }
  24.         catch (ArgumentNullException)
  25.         {
  26.             Console.WriteLine("No address specified. Please input correct address.");
  27.         }
  28.         catch (WebException we)
  29.         {
  30.             Console.WriteLine(we.Message);
  31.         }
  32.         catch (NotSupportedException)
  33.         {
  34.             Console.WriteLine("The file is being downloaded. Please wait.");
  35.         }
  36.         finally
  37.         {
  38.             client.Dispose();
  39.             GC.Collect();
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment