C# 使用ZipFile压缩文件时出错
我正在使用Windows窗体应用程序。在我的应用程序中,我使用了C# 使用ZipFile压缩文件时出错,c#,zipfile,C#,Zipfile,我正在使用Windows窗体应用程序。在我的应用程序中,我使用了FolderBrowserDialog,textbox1和两个按钮。在我的文本框中,我正在传递文件夹。它将从文件夹中选择特定的文件类型。在得到这样的文件类型后,我需要使用ZipFile(即icogal.zip)对其进行转换。在检索特定文件类型后,它向我显示FileNotfound错误。出于测试目的,我尝试将检索到的文件显示到listbox,它运行良好。但当我通过ZipFile打电话时,它给了我一个错误,我无法找出错误是什么 name
FolderBrowserDialog
,textbox1
和两个按钮。在我的文本框中,我正在传递文件夹。它将从文件夹中选择特定的文件类型。在得到这样的文件类型后,我需要使用ZipFile(即icogal.zip)对其进行转换。在检索特定文件类型后,它向我显示FileNotfound错误。出于测试目的,我尝试将检索到的文件显示到listbox,它运行良好。但当我通过ZipFile打电话时,它给了我一个错误,我无法找出错误是什么
namespace WinDataStore
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog();
folderBrowserDlg.ShowNewFolderButton = true;
DialogResult dlgResult = folderBrowserDlg.ShowDialog();
if (dlgResult.Equals(DialogResult.OK))
{
textBox1.Text = folderBrowserDlg.SelectedPath;
Environment.SpecialFolder rootFolder = folderBrowserDlg.RootFolder;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
//notification to user
return;
}
string[] extensions = { ".xml",".ddg" };
string[] dizin = Directory.GetFiles(textBox1.Text, "*.*",SearchOption.AllDirectories)
.Where(f => extensions.Contains(new FileInfo(f).Extension.ToLower())).ToArray();
// listBox1.Items.AddRange(dizin);
using (ZipFile zip = new ZipFile())
{
zip.AddFile("dizin", "files");
zip.Save("z.zip");
}
}
}
}
不能将变量作为如下字符串传递:
string[] dizin = ...;
zip.AddFile("dizin", "files");
zip.AddFile(dizin, "files");
而是像这样使用它:
string[] dizin = ...;
zip.AddFile("dizin", "files");
zip.AddFile(dizin, "files");
或者更可能需要循环:
foreach(var file in dizin)
{
zip.AddFile(file, "files");
}
或者,如果您使用的是爱奥尼亚邮政编码库,请使用以下方法:
下面的代码解决了我的问题 使用(ZipFile zip=new ZipFile()) { foreach(dizin中的var文件) { zip.AddFile(文件); } zip.Save(“z.zip”); }什么是
“dizin”
?我猜,您要添加一个数组,但不是名为dizin
.foreach(dizin中的var file){zip.AddFile(file,“files”);}的文件,创建zip文件,但不包括检索到的文件