【问题标题】:webClient DownloadProgressChanged Event not workingwebClient DownloadProgressChanged 事件不工作
【发布时间】:2023-01-18 23:55:30
【问题描述】:

我使用此代码从链接下载文件:

WebClient webClient = new WebClient();
webClient.Headers.Add("Accept: text/html, application/xhtml+xml, */*");
webClient.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
webClient.DownloadFileAsync(new Uri(downloadURL), "C:\\Users\\" + Environment.UserName + "\\Documents\\AudioStreamerUpdate_" + rnd1.ToString() + ".zip");

//track the downloading progress                      
webClient.DownloadProgressChanged += (sender, e) =>
{
    progressBar1.Value = e.ProgressPercentage;
    label1updateinf.Text = e.ProgressPercentage + "%";
    Console.WriteLine(e.ProgressPercentage + "%");
};

由于文件大约 200 Mb,我想跟踪下载进度。

我也试过这段代码:

webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressUpdate);

private void webClient_DownloadProgressUpdate(object sender, DownloadProgressChangedEventArgs e)
{

}

但它给了我这个错误:

“DownloadProgressCallback”没有重载匹配委托“DownloadProgressChangedEventHandler”

【问题讨论】:

    标签: c# winforms c#-7.3


    【解决方案1】:

    如果附加事件处理程序,您的代码应该可以工作调用下载方法:

    public partial class MainForm : Form
    {
        public MainForm() =>InitializeComponent();
        protected override async void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            SetWindowTheme(progressBar1.Handle, string.Empty, string.Empty);
            progressBar1.ForeColor = Color.Aqua;
            progressBar1.BackColor = Color.Gray;
            var path =
                Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                    Application.ProductName,
                    "DB.Browser.for.SQLite-3.12.2-win32.msi"
                );
            Directory.CreateDirectory(Path.GetDirectoryName(path));
    
            using (WebClient webClient = new WebClient())
            {
                // Attach event BEFORE downloading to receive progress
                webClient.DownloadProgressChanged += (sender, e) =>
                {
                    progressBar1.Value = e.ProgressPercentage;
                    label1updateinf.Text = e.ProgressPercentage + "%";
                    Console.WriteLine(e.ProgressPercentage + "%");
                };
                try
                {
                    webClient.DownloadFileAsync(new Uri("https://download.sqlitebrowser.org/DB.Browser.for.SQLite-3.12.2-win32.msi"), path);
                }
                catch (Exception ex)
                {
                    Debug.Assert(false, ex.Message);
                }
            }
        }
        [DllImport("uxtheme", ExactSpelling = true, CharSet = CharSet.Unicode)]
        public extern static Int32 SetWindowTheme(IntPtr hWnd,
                        String textSubAppName, String textSubIdList);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多