public static void Main()
    {
        WebClient downloader = new WebClient();
        using (downloader)
        {
            try
            {

                downloader.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", @"c:\Users\Teodor\Desktop\New folder\test.jpg");

            }
            catch (ArgumentException ae)
            {
                Console.WriteLine("{0} - {1}", ae.GetType(), ae.Message);
            }
            catch (WebException webEx)
            {
                Console.WriteLine("{0} - {1}", webEx.GetType(), webEx.Message);
                Console.WriteLine("Destination not found!");
            }
            catch (NotSupportedException supportEx)
            {
                Console.WriteLine("{0} - {1}", supportEx.GetType(), supportEx.Message);
                Console.WriteLine(supportEx.Message);
            }
            catch (Exception allExp)
            {
                Console.WriteLine("{0} - {1}", allExp.GetType(), allExp.Message);
            }
        }
    }
    static void Download(string url, string downloadDir)
    {
        try
        {
            WebClient client = new WebClient();

            using (client)
            {
                client.DownloadFile(url, downloadDir);
            }

            Console.WriteLine("The file was successfully downloaded!!!");
        }
        catch (ArgumentNullException)
        {
            Console.Error.WriteLine("Missing url or download directory!!!");
        }
        catch (WebException)
        {
            Console.Error.WriteLine("You have entered a wrong url or you have not set a name for the file!!!");
        }
        catch (NotSupportedException)
        {
            Console.Error.WriteLine("The application can not download the file!!!");
        }
    }
Example #3
0
    static void Main()
    {
        Console.WriteLine(@"Problem 4. Download file
Write a program that downloads a file from Internet (e.g. Ninja image) 
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.
============================================================================
Solution:");
        string sourceResource = "http://blogs.telerik.com/images/default-source/miroslav-miroslav/super_ninja.png?sfvrsn=2";
        string localFileName = Path.GetFileName(sourceResource);
        using (WebClient myWebClient = new WebClient())
        {
            try
            {
                Console.WriteLine("Start downloading {0}", sourceResource);
                myWebClient.DownloadFile(sourceResource, localFileName);
                Console.WriteLine("Download succesfull.");
                Console.WriteLine("You can see downloaded file in: local folder of this program\\bin\\Debug\\");
            }
            catch (WebException ex)
            {
                Console.Write(ex.Message);
                if (ex.InnerException != null)
                    Console.WriteLine(" " + ex.InnerException.Message);
                else
                    Console.WriteLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Something going wrong. Details: " + ex.Message);
            }
        }
    }
    static void Main()
    {
        WebClient webClient = new WebClient();
            using (webClient)
            {
                try
                {
                    webClient.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", @"..\..\Logo-BASD.jpg");
                    //this row will be executed only if downwoad is successfull
                    Console.WriteLine("The file is downloaded");
                }
                catch (ArgumentNullException ex1)
                {
                    Console.WriteLine("{0}",ex1.Message);
                }
                catch (System.Net.WebException ex2)
                {
                    Console.WriteLine("{0}", ex2.Message);
                }
                catch (System.NotSupportedException ex3)
                {
                    Console.WriteLine("{0}", ex3.Message);
                }

            }
    }
Example #5
0
    static void Main()
    {
        using (WebClient web = new WebClient())
        {
            try
            {
                // Download and save the file
                web.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", @"D:\Logo-BASD.jpg");
            }
            catch (WebException)
            {
                Console.WriteLine("An error occurred while downloading data, or the file does not exist.");
            }
            catch (NotSupportedException)
            {
                Console.WriteLine("The method has been called simultaneously on multiple threads.");
            }

            Console.Write("[Done] The file is saved in 'D:\\'" + Environment.NewLine +
                              " Open file? [Y/N]: ");

            // open file option
            switch (Console.ReadLine().ToLower())
            {
                case "y": OpenFile(); break;
                default: return;
            }
        }
    }
    static void Main()
    {
      using (WebClient downloadClient = new WebClient())
        {
            try
            {
                Console.WriteLine("Downloading..");
                downloadClient.DownloadFile("http://telerikacademy.com/Content/Images/news-img01.png", "news-img01.png");
                Console.WriteLine("Image was downloaded successfully in 'bin' directory of the project!");
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (NotSupportedException ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("Goodbye!");
            }
        }

    }
    static void Main()
    {
        Console.Write("Enter URL address: ");
        string path = Console.ReadLine();

        Console.Write("Enter file name: ");
        string name = Console.ReadLine();

        using (WebClient webClient = new WebClient())
        {
            try
            {
                using (WebClient Client = new WebClient())
                {
                    Client.DownloadFile(path, name);
                }
            }
            catch (ArgumentException)
            {
                Console.WriteLine("The path is zero-length/ one or more invalid chars!");
            }
            catch (WebException)
            {
                Console.WriteLine("Invalid address/ filename-null reference(empty)/ error while downloading!");
            }
            catch (System.Security.SecurityException)
            {
                Console.WriteLine("You do not have the required permission to write to local file!");
            } 
        }
    }
    static void Main()
    {
        WebClient webClient = new WebClient();

        try
        {
            webClient.DownloadFile(internetResource, resourceName);
            Console.WriteLine("downloading to: {0}",Path.GetFullPath(resourceName));
        }
        catch (System.ArgumentNullException ex)
        {
            Console.WriteLine(ex.Message.ToString());
        }
        catch (System.Net.WebException ex)
        {
            Console.WriteLine(ex.Message.ToString());
        }
        catch (System.NotSupportedException ex)
        {
            Console.WriteLine(ex.Message.ToString());
        }
        finally
        {
            FreeResources();
        }
    }
    static void Main()
    {
        try
        {
            WebClient webCilend = new WebClient();

            // feel free to change the adress
            string url = "http://telerikacademy.com/Content/Images/news-img01.png";
            string fileName = "TelerikNinja.png";

            webCilend.DownloadFile(url, fileName);
            Console.WriteLine("The picture is saved at {0}.", Directory.GetCurrentDirectory());
        }
        catch (UnauthorizedAccessException exception)
        {
            Console.WriteLine(exception.Message);
        }
        catch (NotSupportedException exception)
        {
            Console.WriteLine(exception.Message);
        }
        catch (WebException exception)
        {
            Console.WriteLine(exception.Message);
        }
        catch (ArgumentException exception)
        {
            Console.WriteLine(exception.Message);
        }
    }
Example #10
0
    static void Main()
    {
        string link = "http://www.devbg.org/img/Logo-BASD.jpg";
        string file = "Logo-BASD.jpg";

        using (WebClient myClient = new WebClient())
        {
            try
            {
                myClient.DownloadFile(link, file);
                Console.WriteLine(@"The file is downloaded in project subfolder ...\04.FileDownload\bin\debug");
            }
            catch (ArgumentException)
            {
                Console.WriteLine("The web-address name is either empty, or read as empty. Please provide a valid address.");
            }
            catch (WebException)
            {
                Console.WriteLine("The web-address was not found. ");
                Console.WriteLine("Please make sure the address is correct.");
                Console.WriteLine("Please check your network connection.");
            }
            catch (NotSupportedException)
            {
                Console.WriteLine("This operation is already performed by another method.");
            }
            catch (SecurityException)
            {
                Console.WriteLine("Security Issue. You do not have permission to access this file.");
            }
        }
    }
 static void Main()
 {
     Console.WriteLine("This program downloads a file from internet\n" +
         "by given URL from the user.");
     Console.Write("Please enter URL address: ");
     string URL = Console.ReadLine();
     Console.Write("Please enter file name: ");
     string file = Console.ReadLine();
     string fullAddress = URL + "/" + file;
     WebClient webClient = new WebClient();
     try
     {
         webClient.DownloadFile(fullAddress, file);
     }
     catch (ArgumentNullException)
     {
         Console.WriteLine("The address can not be null");
     }
     catch (WebException)
     {
         Console.WriteLine("Invalid address or empty file.");
     }
     catch (NotSupportedException)
     {
         Console.WriteLine("This method does not support simultaneous downloads.");
     }
     finally
     {
         webClient.Dispose();
     }
 }
Example #12
0
    static void Main()
    {
        using (WebClient Client = new WebClient())
        {
            try
            {
                Client.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", "pornpic.jpeg");
            }

            catch(ArgumentException)
            {
                Console.WriteLine("Empty url entered.");
            }

            catch(System.Net.WebException)
            {
                Console.WriteLine("Not valid url or target file entered.");
            }

            catch(Exception)
            {
                Console.WriteLine("FATAL ERROR! RUNN BEFORE IT LAYS EGGS");
            }

            //no need of finally because we use "using" keyword :P
        }
    }
    static void Main(string[] args)
    {
        string url = "http://telerikacademy.com/Content/Images/news-img01.png";
        string fileName = "TelerikNinja.png";
        WebClient downloadPic = new WebClient();

        try
        {
            downloadPic.DownloadFile(url, fileName);
            Console.WriteLine("The picture is saved at {0}.", Directory.GetCurrentDirectory());
        }
        catch (UnauthorizedAccessException exception)
        {
            Console.WriteLine(exception.Message);
        }
        catch (NotSupportedException exception)
        {
            Console.WriteLine(exception.Message);
        }
        catch (WebException exception)
        {
            Console.WriteLine(exception.Message);
        }
        catch (ArgumentException exception)
        {
            Console.WriteLine(exception.Message);
        }
        finally
        {
            downloadPic.Dispose();
        }
    }
Example #14
0
 static void Main()
 {
     using (WebClient client = new WebClient())
     {
         client.DownloadFile("http://api.rethumb.com/v1/width/100/http://factor45.org/images/beach.jpg",  @"beach.thumb.jpg");
     }
 }
    static void Main()
    {
        try
        {
            string url = "http://www.devbg.org/img/";
            string fileName = "Logo-BASD.jpg", myDownload = null;

            WebClient downloader = new WebClient();

            myDownload = url + fileName;
            Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myDownload);

            downloader.DownloadFile(myDownload, fileName);
            Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, url);
            Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);
        }
        catch (ArgumentException)
        {
            Console.WriteLine("There is a problem with the url or file name format entered");
        }

        catch (WebException)
        {
            Console.WriteLine("Combinig the url and filename is invalid or an error occured while downloading data.");
        }
        catch (SecurityException)
        {
            Console.WriteLine("Permission writing issues.");
        }
    }
 static void Main()
 {
     using (WebClient webCl = new WebClient())
     {
         try
         {
             Console.WriteLine("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.");
             Console.WriteLine();
             webCl.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", "image.jpg");
             Console.WriteLine("Download Complete.");
         }
         catch (System.Net.WebException)
         {
             Console.WriteLine("Error downloading file.");
         }
         catch (System.ArgumentException)
         {
             Console.WriteLine("Wrong path");
         }
         finally
         {
             webCl.Dispose();
         }
     }
 }
    static void Main()
    {
        string remoteUri = "http://telerikacademy.com/Content/Images/";
        string fileName = "news-img01.png";
        string webResource = null;
        WebClient myWebClient = new WebClient();
        webResource = remoteUri + fileName;


        Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n", fileName, webResource);
        try
        {
            myWebClient.DownloadFile(webResource, fileName);
            Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, webResource);
        }
        catch (ArgumentNullException)
        {
            Console.WriteLine("No address given.");
        }
        catch (WebException)
        {
            Console.WriteLine("The file does not exist or the name is empty.");
        }
        catch (NotSupportedException)
        {
            Console.WriteLine("The method has been called simultaneously on multiple threads.");
        }
        finally
        {
            myWebClient.Dispose();
        }
        Console.WriteLine();
    }
    // 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()
    {
        WebClient client = new WebClient();
        using (client)
        {
            try
            {
                client.DownloadFile("http://academy.telerik.com/images/default-album/telerik-academy-banner-300x250.jpg?sfvrsn=2",
                            "../../../telerik-academy-banner-300x250.jpg");
            }
            catch (WebException we)
            {
                Console.Error.WriteLine("File is inaccessible!" + we.Message);
            }

            catch (NotSupportedException ne)
            {
                Console.Error.WriteLine("Ooops something went wrong!" + ne.Message);
            }
            catch (ArgumentNullException ae)
            {
                Console.Error.WriteLine("Ooops something went wrong!" + ae.Message);
            }
        }
    }
    static void Main()
    {
        WebClient webClient = new WebClient();
        string linkToFile = @"http://www.devbg.org/img/Logo-BASD.jpg";
        try
        {
            webClient.DownloadFile( linkToFile,"logo.jpg");
        }

        catch (ArgumentNullException)
        {
            Console.WriteLine("You can't use null arguments");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("'{0}' is a wrong path", linkToFile);
        }
        catch (NotSupportedException)
        {
            Console.WriteLine("Use different slashes for this operating system");
        }
        catch (WebException)
        {
            Console.WriteLine("Not file specified to save to");
        }
        finally
        {
            webClient.Dispose();
        }
    }
Example #20
0
 static void Main()
 {
     try
     {
         WebClient webClient = new WebClient();
         webClient.DownloadFile("http://telerikacademy.com/Content/Images/news-img01.png", "news-img01.png");
         Console.WriteLine("You can find the downloaded image in 'bin' directory of the project.");
     }
     catch (ArgumentException ex)
     {
         Console.WriteLine(ex.Message);
     }
     catch (WebException ex)
     {
         Console.WriteLine(ex.Message);
     }
     catch (NotSupportedException ex)
     {
         Console.WriteLine(ex.Message);
     }
     catch (UnauthorizedAccessException ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
 static void Main()
 {
     Console.WriteLine("Enter the URL that you wish to download.");
     Console.Write("URL: ");
     string url = Console.ReadLine();
     Console.WriteLine("Directory to download to:");
     string directory = Console.ReadLine();
     try
     {
         WebClient request = new WebClient();
         using (request)
         {
             request.DownloadFile(url, directory);
         }
     }
     catch (ArgumentNullException)
     {
         Console.WriteLine("The url does not exist or it was not entered.");
     }
     catch (ArgumentException)
     {
         Console.WriteLine("Invalid input data.");
     }
     catch (WebException)
     {
         Console.WriteLine("You do not have privileges to download / Problem with internet connection.");
     }
     catch (NotSupportedException)
     {
         Console.WriteLine("Error 404: File not found!");
     }
 }
Example #22
0
    static void Main()
    {
        using (WebClient webClient = new WebClient())
        {
            try
            {
                Console.WriteLine("Starting to download...");
                webClient.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", "../../Logo-BASD.jpg");
                Console.WriteLine("Downloaded!");
            }

            catch (WebException ex)
            {
                Console.Error.WriteLine("Sorry, there was an error!" + ex.Message);
            }
            catch (ArgumentNullException ex)
            {
                Console.Error.WriteLine("Sorry, you didn't enter anything!" + ex.Message);
            }
            catch (NotSupportedException ex )
            {
                Console.Error.WriteLine("Sorry, there was an error!" + ex.Message);
            }
        }
    }
Example #23
0
    static void Main(string[] args)
    {
        /*     Write a program that downloads a file from Internet (e.g. Ninja image) and stores it in 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.*/

            Console.WriteLine("Enter Internet file address: "); // https://telerikacademy.com/Content/Images/news-img01.png
            string internetAddress = Console.ReadLine();
            string extension = internetAddress.Substring(internetAddress.LastIndexOf('.'), internetAddress.Length - internetAddress.LastIndexOf('.'));

            using (WebClient internetRover = new WebClient())
            {
                try
                {
                    Console.WriteLine("Recieving data...");
                    internetRover.DownloadFile(internetAddress, "downloadedFile" + extension);
                    Console.WriteLine("File successfully downloaded! File Location:...\\bin\\Debug");
                }
                catch (ArgumentNullException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                catch (WebException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                catch (NotSupportedException ex)
                {
                    Console.WriteLine(ex.Message);
                }

            }
    }
    static void Main()
    {
        try
        {
            WebClient webClient = new WebClient();
            webClient.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", @"C:\");
        }

        catch (ArgumentNullException)
        {
            Console.WriteLine("The address parameter is null.");
        }

        catch (WebException)
        {
            Console.Write("The URI formed by combining BaseAddress and address is invalid");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(" or ");
            Console.ResetColor();
            Console.Write("filename is null or Empty");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(" or ");
            Console.ResetColor();
            Console.Write("the file does not exist");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(" or ");
            Console.ResetColor();
            Console.WriteLine("an error occurred while downloading data.");
        }

        catch (NotSupportedException)
        {
            Console.WriteLine("The method has been called simultaneously on multiple threads.");
        }
    }
Example #25
0
 static void Main()
 {
     WebClient webClient = new WebClient();
         try
         {
             webClient.DownloadFile("http://telerikacademy.com/Content/Images/news-img01.png", @"news-img01.png");
             Console.WriteLine("Done"); // image is downloaded in your current directory.
         }
         catch (ArgumentNullException)
         {
             Console.WriteLine("The address parameter is null");
         }
         catch (WebException)
         {
             Console.WriteLine("File does not exist, its name is null or empty, address is invalid, or an error occurred while downloading data");
         }
         catch (NotSupportedException)
         {
             Console.WriteLine("The method has been called simultaneously on multiple threads");
         }
         finally
         {
             webClient.Dispose();
         }
 }
 static void Main()
 {
     string decorationLine = new string('-', 80);
     Console.Write(decorationLine);
     Console.WriteLine("***Downloading a file from Internet and storing it in the current directory***");
     Console.Write(decorationLine);
     WebClient webClient = new WebClient();
     try
     {
         string remoteUri = "http://www.telerik.com/images/newsletters/academy/assets/";
         string fileName = "ninja-top.gif";
         string fullFilePathOnWeb = remoteUri + fileName;
         webClient.DownloadFile(fullFilePathOnWeb, fileName);
         Console.WriteLine("Downloading '{0}' from {1} is completed!", fileName, remoteUri);
         Console.WriteLine("You can find it in {0}.", Environment.CurrentDirectory);
     }
     catch (ArgumentNullException)
     {
         Console.WriteLine("No file address is given!");
     }
     catch (WebException webException)
     {
         Console.WriteLine(webException.Message);
     }
     catch (NotSupportedException)
     {
         Console.WriteLine("Downloading a file is in process! Cannot download multiple \nfiles simultaneously!");
     }
     finally
     {
         webClient.Dispose();
     }
 }
Example #27
0
 static void Main()
 {
     WebClient webClient = new WebClient();
     try
     {
         webClient.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", @"C:\Users\Konstantin Iliev\Documents\txt.txt");
     }
     catch (ArgumentException ae)
     {
         Console.WriteLine("{0} - {1}", ae.GetType(), ae.Message);
     }
     catch (WebException webEx)
     {
         Console.WriteLine("{0} - {1}", webEx.GetType(), webEx.Message);
         Console.WriteLine("Destination not found!");
     }
     catch (NotSupportedException supportEx)
     {
         Console.WriteLine("{0} - {1}", supportEx.GetType(), supportEx.Message);
         Console.WriteLine(supportEx.Message);
     }
     catch (Exception allExp)
     {
         Console.WriteLine("{0} - {1}", allExp.GetType(), allExp.Message);
     }
 }
 static void Main()
 {
     Console.WriteLine("Enter URI(adress) from where you will download: ");
     string url = Console.ReadLine();
     Console.WriteLine("Enter name for your file: ");
     string fileName = Console.ReadLine();
     //using  closes the resource after it`s work is done
     using (WebClient webClient = new WebClient())
     {
         try
         {
             webClient.DownloadFile(url, fileName);
             Console.WriteLine("Download complete!");
         }
         catch (ArgumentNullException)
         {
             Console.WriteLine("Either adress parameter is null or filename parameter is null!");
         }
         catch (WebException)
         {
             Console.WriteLine("The URI adress is invalid,filename is null or empty or an error occurred while downloading data!");
         }
         catch (NotSupportedException)
         {
             Console.WriteLine("The method has been called simultaneously on multiple threads.");
         }
         catch (ArgumentException)
         {
             Console.WriteLine("The path is not of a legal form.");
         }
     }
 }
 static void Main()
 {
     try
     {
         string uri = "http://www.devbg.org/img/";
         string fileName = "Logo-BASD.jpg";
         string WebResource = null;
         WebClient myWebClient = new WebClient();
         WebResource = uri + fileName;
         Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, WebResource);
         myWebClient.DownloadFile(WebResource, fileName);
         Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, WebResource);
         Console.WriteLine("\nDownloaded file saved in the current file system(bin) folder");
     }
     catch (ArgumentNullException)
     {
         Console.WriteLine("Invalid file");
     }
     catch (WebException)
     {
         Console.WriteLine("No network access");
     }
     catch (NotSupportedException)
     {
         Console.WriteLine("Method is not supported");
     }
 }
Example #30
0
    static void Main()
    {
        // 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.

        using (WebClient webClient = new WebClient())
        {
            try
            {
                string address = "http://www.telerik.com/assets/img/telerik-navigation/telerik-logo.png";
                string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                string fileName = "logo.png";

                webClient.DownloadFile(address, desktop + "\\" + fileName);

                Console.WriteLine("{0} downloaded to your desktop.", fileName);                
            }

            catch (WebException)
            {
                Console.Error.WriteLine("The file does not exist or an error occurred while downloading data.");
            }

            catch (NotSupportedException)
            {
                Console.Error.WriteLine("The method has been called simultaneously on multiple threads.");
            }
        }
    }