下载未调用完整事件处理程序

时间:2017-11-27 02:27:04

标签: c# webclient-download

我有这个C#代码不能完成这项工作。下载完成后,此代码应运行代码。但它不起作用。

代码:

private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    ZipFile.ExtractToDirectory("web/data/main/web.zip", "web/");
}

private void flatToggle2_CheckedChanged(object sender)
{
    if (flatToggle2.Checked)
    {
        //Create File
        Directory.CreateDirectory("web/data/main/");
        timer2.Start();
        bunifuProgressBar1.Show();
        bunifuCustomLabel1.Show();

        //Downloand
        using (var client = new WebClient())
        {
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadFileCompleted);
            client.DownloadFile("https://www.apachelounge.com/download/VC15/binaries/httpd-2.4.29-Win64-VC15.zip", "web/data/main/web.zip");
        }
    }
    else
    {               
    }
}

这是无效的代码

private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    ZipFile.ExtractToDirectory("web/data/main/web.zip", "web/");
}

我尝试在没有解压缩的情况下运行此代码,但它无效,所以我需要有关此代码的帮助。

1 个答案:

答案 0 :(得分:0)

查看文档,DownloadFile()是同步的,因此不需要回调。请改为尝试:

using (var client = new WebClient())
{
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadFileCompleted);
    client.DownloadFile("https://www.apachelounge.com/download/VC15/binaries/httpd-2.4.29-Win64-VC15.zip", "web/data/main/web.zip");
    ZipFile.ExtractToDirectory("web/data/main/web.zip", "web/");
}

这应该足够了。