所以我要做的是从URI下载zip文件,并将其提取到某个目录中,但我收到了这个错误:
"System.IO.IO异常:";文件"C:\Users\yup_C\source\repos\LauncherYCFPS\bin\Debug\net5.0-windows\new_ver.zip"已存在">
所以我认为这是由wc.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFinished);
引起的,因为它工作不正常,而static void DownloadFinished(object sender, EventArgs e)
在没有等待文件下载的情况下启动,我哪里出了问题?
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Data;
using System.Net;
using System.Windows.Forms;
using System.ComponentModel;
namespace LauncherYCFPS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
{
using (WebClient wc = new WebClient())
{
wc.DownloadProgressChanged += wc_DownloadProgressChanged;
wc.DownloadFileAsync(
// Param1 = Link of file
new System.Uri("http://o997388w.beget.tech/new_ver.zip"),
// Param2 = Path to save
@".new_ver.zip"
) ;
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFinished);
}
static void DownloadFinished(object sender, EventArgs e)
{
string startPath = @".";
string zipPath = @".new_ver.zip";
string extractPath = @".extract";
ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);
}
}
// Event to track the progress
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
看看这个错误,已经有一个同名文件了!
在使用ZipFile.ExtractToDirectory方法之前,您必须删除该目录,或者使用将覆盖现有目录的方法:
ZipFile.ExtractToDirectory(zipPath, extractPath, true);
使用允许覆盖文件的重载:
public static void ExtractToDirectory (string sourceArchiveFileName, string destinationDirectoryName, System.Text.Encoding? entryNameEncoding, bool overwriteFiles);