Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- /*
- 4. Write a program that downloads a file from Internet (e.g. http://www.devbg.org/img/Logo-BASD.jpg)
- and stores it the current directory. Find in Google how to download files in C#.
- Be sure to catch all exceptions and to free any used resources in the finally block.
- */
- public class DownloadAFile
- {
- public static void DownloadFile(WebClient client)
- {
- client.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", @"F:\Desktop\Logo-BASD.jpg");
- }
- public static void Main()
- {
- WebClient client = new WebClient();
- try
- {
- DownloadFile(client);
- Console.WriteLine("File downloaded successfully.");
- }
- catch (ArgumentNullException)
- {
- Console.WriteLine("No address specified. Please input correct address.");
- }
- catch (WebException we)
- {
- Console.WriteLine(we.Message);
- }
- catch (NotSupportedException)
- {
- Console.WriteLine("The file is being downloaded. Please wait.");
- }
- finally
- {
- client.Dispose();
- GC.Collect();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment