Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.IO;
- class DownloadFileFromInternet
- {
- //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.
- static void Main()
- {
- Console.WriteLine("Enter the full download url: "); //Ask for the url of the file to download
- string url = Console.ReadLine(); //Store it in the url string
- string fileName = Path.GetFileName(url); //extract the file name
- using (WebClient client = new WebClient())//Open a web client
- {
- try
- {
- client.DownloadFile(url, fileName);//Use the web client with our url and file name
- }
- catch (ArgumentNullException)
- {
- Console.WriteLine("Please provide a url address!"); ;
- }
- catch (WebException)
- {
- Console.WriteLine("An error occurred while trying to download the file! Make sure the url is valid, the file you want to download exists and the internet connection is running!");
- }
- catch (NotSupportedException)
- {
- Console.WriteLine("The DownloadFile method cannot be called simultaneously on multiple threads.");
- }
- finally
- {
- Console.WriteLine("Good bye!");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment