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

Download Complete event handler is not being called

我的 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/");
}

我尝试 运行 不解压缩此代码,但它不起作用,因此我需要有关此代码的帮助。

查看文档,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/");
}

这应该足够了。