我有此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/");
}
这应该就足够了。