(WPF C# 程序)
System.IO.Compression.ZipFile.ExtractToDirectory(File.zip, some_dir, true);
// need to check if unzip is done here (assume it works slow)
File.Delete(File.zip);
您可以只使用await,它会确保任务在移入代码之前完成 阅读有关异步编程
的更多信息private async void UnzipAndDeleteFile(string zipPath, string extractPath)
{
try
{
await Task.Run(() => ZipFile.ExtractToDirectory(zipPath, extractPath, true));
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
MessageBox.Show("File unzipped and deleted successfully.");
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred: {ex.Message}");
}
}