How to download a file from a URL in C#?

前端 未结 11 1279
清歌不尽
清歌不尽 2020-11-22 15:13

What is a simple way of downloading a file from a URL path?

11条回答
  •  一向
    一向 (楼主)
    2020-11-22 15:38

    Include this namespace

    using System.Net;
    

    Download Asynchronously and put a ProgressBar to show the status of the download within the UI Thread Itself

    private void BtnDownload_Click(object sender, RoutedEventArgs e)
    {
        using (WebClient wc = new WebClient())
        {
            wc.DownloadProgressChanged += wc_DownloadProgressChanged;
            wc.DownloadFileAsync (
                // Param1 = Link of file
                new System.Uri("http://www.sayka.com/downloads/front_view.jpg"),
                // Param2 = Path to save
                "D:\\Images\\front_view.jpg"
            );
        }
    }
    // Event to track the progress
    void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar.Value = e.ProgressPercentage;
    }
    

提交回复
热议问题