static void Main()
 {
     WebClient webClient = new WebClient();
     Console.WriteLine("Enter file url: ");
     string downloadSource = Console.ReadLine();
     Console.WriteLine("Enter destination on local hard drive. Specify file name and extension: ");
     string downloadDestination = Console.ReadLine();
     try
     {
         webClient.DownloadFile(downloadSource, downloadDestination);
     }
     catch (System.Net.WebException)
     {
         Console.WriteLine("Error accessing the network.");
     }
     catch (System.NotSupportedException)
     {
         Console.WriteLine("Method not supported or failed attempt to read, seek or write to a stream.");
     }
     catch (ArgumentNullException)
     {
         Console.WriteLine("A null reference cannot be accepted by this method!");
     }
     catch (ArgumentException)
     {
         Console.WriteLine("The path you entered is invalid.");
     }
     finally
     {
         webClient.Dispose();
     }
 }
    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();
        }
    }
    static void Main()
    {
        WebClient webClient = new WebClient();

            using (webClient)
            {
                try
                {
                    Console.WriteLine("Downloading file...");
                    webClient.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", @"d:\myfile.jpg");
                    Console.WriteLine("Done.");
                }
                catch (ArgumentNullException an)
                {
                    Console.WriteLine("ArgumentNullException: " + an.Message);
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine("ArgumentException: " + ae.Message);
                }
                catch (WebException we)
                {
                    Console.WriteLine("WebException: " + we.Message);
                }
                catch (NotSupportedException ns)
                {
                    Console.WriteLine("NotSupportedException: " + ns.Message);
                }
                finally
                {
                    webClient.Dispose();
                    GC.Collect();
                }
            }
    }
 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();
     }
 }
 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();
         }
     }
 }
Example #6
0
 static void Main()
 {
     Console.WriteLine("Enter the URL of the which you want to download:\nExample: \"http://www.d3bg.org/img/upload/tamplier/01.jpg\" ");
     Uri uri = new Uri(Console.ReadLine());
     string fileName = System.IO.Path.GetFileName(uri.LocalPath);
     WebClient webClient = new WebClient();
     try
     {
         // "../../" changes the default location of the downloaded file to two directories above!
         webClient.DownloadFile(uri, "../../" + fileName);
         Console.WriteLine("The file was successfully downloaded!\n!");
     }
     catch (WebException)
     {
         Console.WriteLine("Invalid adress or empty file.");
     }
     catch (NotSupportedException)
     {
         Console.WriteLine("");
     }
     finally
     {
         webClient.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();
    }
Example #8
0
 static void Main()
 {
     string fileName="news-img01.png";
     string path = GetSavePath(fileName);
     string address = "http://telerikacademy.com/Content/Images/news-img01.png";
     WebClient client = new WebClient();
     try
     {
         client.DownloadFile(address, path);
         Console.WriteLine("File saved in {0}", path);
     }
     catch (ArgumentNullException)
     {
         Console.WriteLine("The address can not be null");
     }
     catch (WebException)
     {
         Console.WriteLine("Invalid address or file name.");
     }
     catch (NotSupportedException)
     {
         Console.WriteLine("Simultaneous downloads are not supported.");
     }
     finally
     {
         client.Dispose();
     }
 }
Example #9
0
 private static void ReadAndDownloadFile()
 {
     WebClient wc = new WebClient();
     try
     {
         string link = @"http://www.devbg.org/img/Logo-BASD.jpg";
         wc.DownloadFile(link, "../../pic.jpg");
     }
     catch (ArgumentNullException ane)
     {
         Console.WriteLine(ane.Message);
     }
     catch (WebException we)
     {
         Console.WriteLine(we.Message);
     }
     catch (NotSupportedException nse)
     {
         Console.WriteLine(nse.Message);
     }
     finally
     {
         wc.Dispose();
     }
 }
Example #10
0
    static void Main()
    {
        Console.Write("Please enter the path: ");
        string path = Console.ReadLine();
        string fileName = "ninja.gif";
        WebClient myWebClient = new WebClient();

        try
        {
            myWebClient.DownloadFile(path, fileName);
            Console.WriteLine(@"The file is downloaded in Project\bin\Debug!");
        }
        catch (WebException)
        {
            Console.WriteLine("The adress is invalid.");
        }
        catch (ArgumentNullException)
        {
            Console.WriteLine("The address parameter is null.");
        }
        catch (NotSupportedException)
        {
            Console.WriteLine("The method has been called simultaneously on multiple threads.");
        }
        finally
        {
            myWebClient.Dispose();
        }
    }
    static void Main()
    {
        Console.WriteLine("Task 04 - Download a file from given URI\n\n");

        string uri = "http://www.devbg.org/img/Logo-BASD.jpg";

        Console.WriteLine();

        Console.Write("Please enter full URI to download (or press enter for default): ");
        string file = Console.ReadLine();
        if (file.Length == 0) file = uri;
        WebClient client = null;
        try
        {
            string filename = Path.GetFullPath(".") + @"\" + Path.GetFileName(file);
            Console.WriteLine("\nDownloading {0}\n\nDestination: {1}\n\n", file, filename);
            client = new WebClient();
            client.DownloadFile(file, filename);
        }
        catch (SystemException se)
        {
            Console.WriteLine("Exception: " + se.Message);
        }
        finally
        {
            if (client != null) client.Dispose();
        }

        Console.WriteLine("\n\nPress Enter to finish");
        Console.ReadLine();
    }
 static void Main()
 {
     WebClient client = new WebClient();
     try
     {
         client.DownloadFile("http://telerikacademy.com/Content/Images/news-img01.png", "ninja.png");
     }
     catch (UriFormatException e)
     {
         Console.WriteLine(e.Message);
     }
     catch (HttpListenerException e)
     {
         Console.WriteLine(e.Message);
     }
     catch (WebException e)
     {
         Console.WriteLine(e.Message);
     }
     catch (UnauthorizedAccessException e)
     {
         Console.WriteLine(e.Message);
     }
     catch (NotSupportedException e)
     {
         Console.WriteLine(e.Message);
     }
     finally
     {
         client.Dispose();
     }
 }
 static void Main(string[] args)
 {
     WebClient downloader = new WebClient();
     string uri = "http://www.devbg.org/img/Logo-BASD.jpg";
     string destination = Directory.GetCurrentDirectory() + @"\Logo-BASD.jpg";
     try
     {
         downloader.DownloadFile(uri, destination);
     }
     catch (WebException we)
     {
         Console.WriteLine(we.Message);
     }
     catch (NotSupportedException nse)
     {
         Console.WriteLine(nse.Message); ;
     }
     catch (ArgumentException ae)
     {
         Console.WriteLine(ae.Message);
     }
     finally
     {
         downloader.Dispose();
     }
 }
Example #14
0
    private DateTime GetTimeFromDDG()
    {
        //https://duckduckgo.com/?q=what+time+is+it
        WebClient wc = new WebClient();
        try
        {
            char[] separators = new char[] { '<', '>', '\\', '/', '|' };

            string timeisPage = wc.DownloadString("https://duckduckgo.com/html/?q=what%20time%20is%20it");
            timeisPage = timeisPage.Remove(0, timeisPage.IndexOf("\n\n\t\n\n\n\n            ") + 19);
            string[] timeisSplit = timeisPage.Split(separators, StringSplitOptions.RemoveEmptyEntries);
            string Time = timeisSplit[0].Remove(timeisSplit[0].Length - 5);
            DateTime result;
            if (DateTime.TryParse(Time, out result))
            {
                return result;//.ToString("t");
            }
            throw new Exception();
        }
        catch
        {
            return DateTime.Now;//.ToString("t");
        }
        finally
        {
            wc.Dispose();
            GC.Collect();
        }
    }
Example #15
0
    static void Main()
    {
        WebClient client = new WebClient();

        try
        {
            string adressAsString = "http://www.devbg.org/img/Logo-BASD.jpg";

            string location = "Image.jpg";

            client.DownloadFile(adressAsString, location);

            Console.WriteLine("The download is complete. You can find your file in bin/Debug");
        }
        catch (ArgumentNullException)
        {
            Console.WriteLine("The adress parameter is with null value");
        }
        catch (WebException)
        {
            Console.WriteLine("Invalid adress, the file name is empty or null, or the file does not exist");
        }
        catch (NotSupportedException)
        {
            Console.WriteLine("The method has been called simultaneously on multiple targets");
        }
        finally
        {
            client.Dispose();
        }
    }
    static void Main()
    {
        WebClient downloader = new WebClient();
        string url = "http://www.devbg.org/img/Logo-BASD.jpg";
        string fileName = "downloadedPicture.jpg";

        try
        {
            downloader.DownloadFile(url, fileName);
            Console.WriteLine("The file was successfully downloaded from {0} to the current directory of the program.", url);
        }
        catch (ArgumentNullException)
        {
            Console.WriteLine("The url parameter is null.");
        }
        catch (WebException)
        {
            Console.WriteLine("The fileName is null or Empty, the file does not exist or an error occurred while downloading data.");
        }
        catch (NotSupportedException)
        {
            Console.WriteLine("The method has been called simultaneously on multiple threads.");
        }
        finally
        {
            downloader.Dispose();
        }
    }
 //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(string[] args)
 {
     string url = "http://www.devbg.org/img/Logo-BASD.jpg";
     string localPath = @Environment.CurrentDirectory + "\\Logo-BASD.jpg";
     WebClient webClient = new WebClient();
     try
     {
         webClient.DownloadFile(url, localPath);
     }
     catch (FormatException e)
     {
         Console.WriteLine(e.Message);
         Console.WriteLine(e.Data);
         Console.WriteLine(e.StackTrace);
     }
     catch (AccessViolationException e)
     {
         Console.WriteLine("You cannot save the file in this location {0}", localPath);
         Console.WriteLine(e.Message);
         Console.WriteLine(e.Data);
         Console.WriteLine(e.StackTrace);
     }
     finally
     {
         webClient.Dispose();
     }
 }
    static void Main()
    {
        Console.WriteLine("Enter URL to download from: ");
        string adress = Console.ReadLine();
        Console.WriteLine("Enter name for the file: ");
        string fileName = Console.ReadLine();

        WebClient webClient = new WebClient();
        try
        {
            Console.WriteLine("Downloading file from \"{0}\" .......\n\n", adress);
            // Download the Web resource and save it into the current filesystem folder.
            webClient.DownloadFile(adress, fileName);
            Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, adress);
        }
        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.");
        }
        finally
        {
            webClient.Dispose();
        }
    }
    static void Main()
    {
        // we have some work with namespace Net and webclient class
        WebClient webClient = new WebClient();

        try
        {
            //downloading file from internet
            //first argument is URL from which we will procced
            //second is the name with which we will save it on the comp
            webClient.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", "logo.jpg");
        }
            //signature of exceptions specified for webclient class
        catch(ArgumentNullException ANE)
        {
            Console.WriteLine(ANE.Message);
        }
        catch(WebException WE)
        {
            Console.WriteLine(WE.Message);
        }
        catch(NotSupportedException NSE)
        {
            Console.WriteLine(NSE.Message);
        }
        finally
        {
            webClient.Dispose();
            Console.WriteLine("Good Bye");
        }
    }
 static void Main()
 {
     WebClient webClient = new WebClient();
     Console.Write("Please enter URL: ");
     string url = Console.ReadLine();
     try
     {
         string[] splittedURL = url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
         string name = splittedURL[splittedURL.Length - 1];
         webClient.DownloadFile(url, name);
         Console.WriteLine("The file is downloaded successfuly!");
     }
     catch (ArgumentNullException)
     {
         Console.WriteLine("Please enter valid url!");
     }
     catch (WebException)
     {
         Console.WriteLine("Problem with accessing the network! The url: {0} might be wrong!", url);
     }
     catch (NotSupportedException)
     {
         Console.WriteLine("Does not support the invoked functionality!");
     }
     finally
     {
         webClient.Dispose();
     }
 }
    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 #22
0
    static void Main()
    {
        string address = "http://www.devbg.org/img/Logo-BASD.jpg";
        string fileName = @"d:\Logo-BASD.jpg";
        using (WebClient webClient = new WebClient())

        try
        {
            webClient.DownloadFile(address, fileName);
        }
        catch (ArgumentNullException)
        {
            Console.WriteLine("The address parameter is null.\n-or-\nThe fileName parameter is null");
        }
        catch (WebException)
        {
            Console.WriteLine("The address is invalid.");
        }
        catch (NotSupportedException)
        {
            Console.WriteLine("	The method has been called simultaneously on multiple threads.");
        }
        finally
        {
            webClient.Dispose();
        }
    }
 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 #24
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();
         }
 }
Example #25
0
 protected void Page_Load(object sender, EventArgs e)
 {
     string url = Request.QueryString["url"];
     Uri uri = new Uri(url);
     WebClient wc = new WebClient();
     wc.Encoding = System.Text.Encoding.UTF8;
     string content = wc.DownloadString(uri);
     //content = content.Replace("ruspo.ru", "volotour.ru");
     content = content.Replace("HotTours.aspx", "hot");
     content = content.Replace("href=\"/", "href=\"http://" + uri.Host + "/");
     content = content.Replace("src=\"/", "href=\"http://" + uri.Host + "/");
     Response.Write(content);
     wc.Dispose();
 }
Example #26
0
 static void Main()
 {
     WebClient fileDownload = new WebClient();
     try
     {
         fileDownload.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", "..\\..\\Logo-BASD-downloaded.jpg" );
     }
     catch (Exception exp)
     {
         Console.WriteLine(exp.Message);
     }
     finally
     {
         fileDownload.Dispose();
     }
 }
    static void Main()
    {
        WebClient client = new WebClient();
        string remoteAddress = "";
        string fileName = "";

        try
        {
            Console.WriteLine("Enter the URI of the resource you want to download: ");
            remoteAddress = Console.ReadLine();

            // remoteAddress = "http://www.devbg.org/img/Logo-BASD.jpg";

            Uri uri = new Uri(remoteAddress);

            fileName = Path.GetFileName(uri.LocalPath);
            client.DownloadFile(uri, fileName);

            Console.WriteLine("File {0} was downloaded successfully.", fileName);
        }
        catch (ArgumentNullException)
        {
            Console.WriteLine("The address cannot be null.");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("Path: {0} contains invalid characters.", remoteAddress);
        }
        catch (UriFormatException)
        {
            Console.WriteLine("{0} is invalid Uri format.", remoteAddress);
        }
        catch (WebException)
        {
            Console.WriteLine("The file at: {0} does not exists, or address is invalid.",
                remoteAddress);
        }
        catch (NotSupportedException)
        {
            Console.WriteLine("Method {0} has been called simultaneously on multiple threads.",
                "WebClient.DownloadFile");
        }
        finally
        {
            client.Dispose();
        }
    }
Example #28
0
    // 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.
    private static void Main()
    {
        Console.WriteLine("Enter the file (name and extension) to download");
        string file = Console.ReadLine();   // news-img01.png

        Console.WriteLine("Enter the address to that file");
        string URL = Console.ReadLine();    // http://telerikacademy.com/Content/Images/news-img01.png

        WebClient wc = new WebClient();
        try
        {
            wc.DownloadFile(URL, file);

            Console.Clear();
            Console.WriteLine("DONE");
            Console.WriteLine("(file is saved in the application's start directory)");
        }
        catch (ArgumentNullException ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine("Try again!");
            Main();
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine("Try again!");
            Main();
        }
        catch (WebException ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine("Try again!");
            Main();
        }
        catch (NotSupportedException ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine("Try again!");
            Main();
        }
        finally
        {
            wc.Dispose();
        }
    }
Example #29
0
    public static void PostHighScore(string name, long score)
    {
        try
        {
            WebClient   webClient = new WebClient();

            webClient.Headers.Add("Content-Type", "application/json");

            webClient.UploadStringAsync(_updateUri, "{ name : '" + name + "', score : " + score + " }");

            webClient.Dispose();
        }
        catch(System.Exception ex)
        {
            Debug.Log(ex.Message);
        }
    }
Example #30
0
 static void Main()
 {
     using (WebClient webClient = new WebClient())
     {
         try
         {
             webClient.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", @"d:\Logo-BASD.jpg");
         }
         catch (Exception e)
         {
             Console.WriteLine("Exception : {0} : {1}", e.GetType().Name, e.Message);
         }
         finally
         {
             webClient.Dispose();
         }
     }
 }