Home > Software engineering >  Using .net core to download a file from internet
Using .net core to download a file from internet

Time:10-16

I'm trying to download a file from internet to local folder with .net core, although it compiles without warnings or errors when executed it shows an error

using System;

namespace download_file
{
    class Program
    {
        static void Main(string[] args)
        {
            string zip_url = @"https://www.python.org/ftp/python/3.9.5/python-3.9.5-embed-amd64.zip";
            string current_directory = System.IO.Directory.GetCurrentDirectory();
            string dl_path = System.IO.Directory.CreateDirectory(System.String.Format(@"{0}\DL", current_directory)).FullName;
            
            string[] zip_url_words = zip_url.Split('/', 1);
            string downloaded_file_path = System.String.Format(@"{0}\{1}", dl_path, zip_url_words[zip_url_words.Length - 1]);

            // downloading the embedded python zip file
            file_download(zip_url, downloaded_file_path);

            void file_download(string url, string save_path)
            {
                if (url is null)
                {
                    return;
                }
                string[] url_word_array = url.Split('/', 1);

                if (save_path is null)
                {
                    save_path = System.IO.Directory.GetCurrentDirectory()   @"\"   url_word_array[url_word_array.Length - 1];  //rsplit
                }
                System.Net.WebClient client = new System.Net.WebClient();
                client.DownloadFile(url, save_path);
                //System.Console.WriteLine("Press any key...")
                //System.Console.ReadLine()
            }
        }
    }
}

enter image description here

CodePudding user response:

The problem is in this line:

string[] zip_url_words = zip_url.Split('/', 1);

From the documentation:

    public String[] Split(char[]? separator, int count);
    //
    // Summary:
    //     Splits a string into a maximum number substrings based on the provided character
    //     separator.

If you add 1 as the second parameter, then you're asking the function to split the string with the separator but return "only one" value in the resulting array. The function then returns an array with only the original string because that is only way it can think to split the array with exactly-one element in the array!

Therefore the above code returns ["https://www.python.org/ftp/python/3.9.5/python-3.9.5-embed-amd64.zip"] as the value for the variable zip_url_words. Change the above code like so:

string[] zip_url_words = zip_url.Split('/');

When you make this change, then the function returns an array with the split that you expect ['https:','','www.python.org','ftp','python','3.9.5','python-3.9.5-embed-amd64.zip']

I made the change and the code works for me.

CodePudding user response:

I changed

string[] zip_url_words = zip_url.Split('/', 1);

to

string[] zip_url_words = zip_url.Split('/', '1');

and

string[] url_word_array = url.Split('/', 1);

to

string[] url_word_array = url.Split('/', '1');

I downloaded
Could you try one more time?

  • Related