How to use the ResourceDownloaded class to download files using httpclient?

Daniel Zedek 0 Reputation points
2023-03-19T16:34:25.7666667+00:00

The problem is in this line I'm not sure what should I give it. I have in the designer of form1 a progressBar.

await ResourceDownloader.DownloadWithProgress.ExecuteAsync(client, list[i], textBoxRadarPath.Text, , () =>

what should I put for the downloadprogresshandler ? and then after it for the next parameter?

I want to use the httpclient with the ResourceDownloaded class to download images with a progressbar and to report to label/s information's like download speed and amount left to download for each file.

usage in form1:

using Ookii.Dialogs.WinForms;
using System.Text;

namespace Downloading_Files
{
    public partial class Form1 : Form
    {
        List<string> list = new List<string>();

        public Form1()
        {
            InitializeComponent();

            textBoxRadarPath.Text = Properties.Settings.Default.RadarFolder;
        }

        internal List<string> Sites()
        {
            

            Radar radar = new Radar(textBoxRadarPath.Text);
            radar.PrepareLinks();

            foreach (var link in radar.links)
            {
                list.Add(link);
            }
            // [...] add more URLs
            return list;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        int counter = 0;
        private async void btnStartDownload_Click(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();

            for (int i = 0; i < list.Count; i++)
            {
                await ResourceDownloader.DownloadWithProgress.ExecuteAsync(client, list[i], textBoxRadarPath.Text, , () =>
                {
                    var requestMessage = new HttpRequestMessage(HttpMethod.Get, list[i]);
                    requestMessage.Headers.Accept.TryParseAdd("application/octet-stream");
                    return requestMessage;
                });
            }
        }

        private void btnCancelDownload_Click(object sender, EventArgs e)
        {

        }

        private void btnDownloadFolder_Click(object sender, EventArgs e)
        {
            RadarFolderDialog();
        }

        private void RadarFolderDialog()
        {
            VistaFolderBrowserDialog dlg = new VistaFolderBrowserDialog();
            dlg.ShowNewFolderButton = true;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (!dlg.SelectedPath.EndsWith("Radar"))
                {
                    if (!Directory.Exists(dlg.SelectedPath))
                    {
                        Directory.CreateDirectory(dlg.SelectedPath + "\\Radar");
                    }

                    Properties.Settings.Default.RadarFolder = dlg.SelectedPath;// + "\\Radar";
                    textBoxRadarPath.Text = dlg.SelectedPath;// + "\\Radar";
                    Properties.Settings.Default.Save();
                }
                else
                {
                    Properties.Settings.Default.RadarFolder = dlg.SelectedPath;
                    textBoxRadarPath.Text = dlg.SelectedPath;
                    Properties.Settings.Default.Save();
                }
            }
        }
    }
}

I have this class and I want to use it in form1 to download files.

In this link for some reason, I can't add the code in here:

https://pastebin.com/Bprirs47

I saw the answer here in this link and this is what I want to do but I'm not sure how to use it in form1.

https://stackoverflow.com/questions/74544755/how-to-download-files-using-httpclient-with-a-progressbar


Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,838 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.