我正在创建Windows窗体。我正在使用folderbrowserdialog,textbox和button。我正在从所有子目录中检索特定类型的文件,并将它们存储在新文件夹中,而不是压缩该文件。我想知道我的代码有什么问题,它创建文件夹,zipfile但新创建的文件夹和zip文件中没有文件。
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 button2_Click(object sender, EventArgs e)
{
string[] extensions = { ".xml",".ddg" };
string[] d = Directory.GetFiles(textBox1.Text, "*.*", SearchOption.AllDirectories)
.Where(f => extensions.Contains(new FileInfo(f).Extension.ToLower())).ToArray();
//listBox1.Items.AddRange(d);
var dir = @"D:\New folderl2221"; // folder location
if (!Directory.Exists(dir)) // if it doesn't exist, create
Directory.CreateDirectory(dir);
foreach (var file in d)
{
FileInfo mFile = new FileInfo(file);
// to remove name collusion
if (new FileInfo(dir + "\\" + mFile.Name).Exists == false)
mFile.MoveTo(dir + "\\" + mFile.Name);
}
using (ZipFile zip = new ZipFile())
{
zip.AddFile(s, "files");
zip.AddDirectory(dir);
zip.Save(@"D:\Ajax11.zip");
}
}