找不到ExtractToDirectory源,路径,布尔值C#VS 2019

问题描述

我正在尝试使用ExtractToDirectory函数,并且在不存在文件的情况下可以正常工作。 (如果没有相同的文件名,则可以解压缩) 检查Microsoft网站并查看ExtractToDirectory之后,我发现应该显示更多2个重载函数

我所能使用的是: (源,路径)和(源,路径,编码) 没有找到布尔值(应该显示是否允许覆盖)

解决方法

我看到方法System.IO.Compression.ZipFile.ExtractToDirectory(String,String,Boolean)仅在 .NET Core 2.0 之后可用,而 .NET Framework .NET Standard 都不可用>。

它将在 .NET 5 上可用。

因此,在 .NET Framework 上,您需要先删除现有文件夹的文件,然后再提取...或使用其他开发人员的另一个程序集:

using System.IO;
using System.IO.Compression;

if ( Directory.Exists(extractToPath) )
  // Ask user confirmation if needed
  Directory.Delete(extractToPath,true);

ZipFile.ExtractToDirectory(zipSourcePath,extractToPath);

System.IO.Directory.Delete()

请不要忘记将项目中的引用添加到程序集System.IO.Compression.FileSystem中(请参见下图)。

仅更新目标文件夹

有了zip文件中的新文件,您可以将zip文件解压缩到用户temp文件夹的子文件夹中,然后将其移动到您要更新树的文件夹中。

它应该满足您的需求。

string tempPath = System.IO.Path.GetTempPath();

ZipFile.ExtractToDirectory(zipSourcePath,tempPath);

接下来的用途:

Directory.Move doesn't work (file already exist)

MoveDirectory(tempPath,extractToPath);

因为Directory.Move(string,string) Method不能使用,因为它不允许覆盖。

如果您遇到困难,请随时提出一个新问题。

我希望这可以帮助您享受C#编码:

How do I improve my knowledge in C#


enter image description here

enter image description here

enter image description here

enter image description here