System.Net.WebClient.DownloadFileTaskAsync C# (CSharp) Method

DownloadFileTaskAsync() public method

public DownloadFileTaskAsync ( System address, string fileName ) : System.Threading.Tasks.Task
address System
fileName string
return System.Threading.Tasks.Task
        public System.Threading.Tasks.Task DownloadFileTaskAsync(System.Uri address, string fileName) { throw null; }
        public string DownloadString(string address) { throw null; }

Same methods

WebClient::DownloadFileTaskAsync ( string address, string fileName ) : System.Threading.Tasks.Task

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Downloads a file from the internet to a directory Asynchronously.
        /// </summary>
        /// <param name="URL">The URL of the file to download.</param>
        /// <param name="downloadDirectory">The directory to download the file to.</param>
        /// <param name="overwrite">Whether to overwrite what's there</param>
        /// <param name="errorActions">Actions to pass to showError on main error.</param>
        /// <param name="silent">Whether to show messages when it starts downloading or if the file allready existed</param>
        /// <param name="ignoreError">Whether to error if the main try loop fails.</param>
        /// <param name="specifyDownloadFile">Whether the downloadDirectory includes the file name to download to</param>
        public static async Task<bool> downloadFileAsync(string URL, string downloadDirectory, bool overwrite = false, bool silent = false, bool specifyDownloadFile = false, bool ignoreError = false, string[] errorActions = null)
        {

            //Get filename from URL
            string filename = Path.GetFileName(new Uri(URL).AbsolutePath);
            string dir = Path.GetDirectoryName(downloadDirectory);

            //If the file exists check if overwrite is accepted
            if (!(File.Exists(downloadDirectory + "/" + filename) | File.Exists(downloadDirectory)) || overwrite)
            {
                if (!specifyDownloadFile)
                {
                    //If the directory doesn't exist, create it
                    if (!Directory.Exists(downloadDirectory))
                    {
                        Logging.logMessage("Created directory " + downloadDirectory, 2);
                        Directory.CreateDirectory(downloadDirectory);
                    }
                }
                else
                {
                    //If the directory doesn't exist, create it
                    if (!Directory.Exists(dir))
                    {
                        Logging.logMessage("Created directory " + dir, 2);
                        Directory.CreateDirectory(dir);
                    }
                }

                //Acctually download file
                try
                {
                    if (!silent) Logging.logMessage("Trying to download " + URL + " to " + downloadDirectory, 2);
                    WebClient wc = new WebClient();
                    if (specifyDownloadFile)
                    {
                        await wc.DownloadFileTaskAsync(new Uri(URL), downloadDirectory);
                    }
                    else
                    {
                        await wc.DownloadFileTaskAsync(new Uri(URL), downloadDirectory + "/" + filename);
                    }
                    wc.Dispose();
                    wc = null;
                }
                catch (Exception e)
                {
                    if (!ignoreError) Logging.showError("Failed to download " + URL + " :" + e.ToString(), errorActions);
                    return false;
                }
            } else {
                if (!silent) Logging.logMessage("Didn't download " + URL + " to " + downloadDirectory + " because it already existed", 2);
            }
            return true;
        }
All Usage Examples Of System.Net.WebClient::DownloadFileTaskAsync