Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Net;
- using System.Security;
- class DownloadingFile
- {
- static void Main()
- {
- try
- {
- string remoteUri = "http://www.devbg.org/img/";
- string fileName = "Logo-BASD.jpg";
- string outputFile = @"C:\Downloads\Logo-BASD.jpg";
- string WebResource = null;
- // Create output directory (if necessary)
- string outputFolder = Path.GetDirectoryName(outputFile);
- if (!Directory.Exists(outputFolder))
- {
- Directory.CreateDirectory(outputFolder);
- }
- WebClient myWebClient = new WebClient();
- // Concatenate the domain with the Web resource filename.
- WebResource = remoteUri + fileName;
- Console.WriteLine("Downloading File \"{0}\" from:\n\"{1}\" .......\n\n", fileName, WebResource);
- // Download the Web resource and save it into the current filesystem folder.
- myWebClient.DownloadFile(WebResource, outputFile);
- Console.WriteLine("Successfully Downloaded File \"{0}\" from:\n\"{1}\"", fileName, WebResource);
- Console.WriteLine("\nDownloaded file saved in the following file system folder:\n" + outputFile);
- }
- catch (ArgumentNullException)
- {
- Console.WriteLine("Address or filename parameter is null!");
- }
- catch (WebException)
- {
- Console.WriteLine("Error, becouse one of the following reason:");
- Console.WriteLine("- The URI formed by combining BaseAddress and address is invalid.");
- Console.WriteLine("- Filename is a null reference, or Empty.");
- Console.WriteLine("- An error occurred while downloading data.");
- }
- catch (NotSupportedException)
- {
- Console.WriteLine("The method has been called simultaneously on multiple threads.");
- }
- catch (SecurityException)
- {
- Console.WriteLine("You don't have permission to write to local file!");
- }
- }
- }
Add Comment
Please, Sign In to add comment