private void btn_Backup_Click(object sender, EventArgs e)
{
List<DirectoryInfo> SourceDir = this.lbox_Sources.Items.Cast<DirectoryInfo>().ToList();
List<DirectoryInfo> TargetDir = this.lbox_Targets.Items.Cast<DirectoryInfo>().ToList();
foreach (DirectoryInfo sourcedir in SourceDir)
{
foreach (DirectoryInfo targetdir in TargetDir)
{
string dateString = DateTime.Now.ToString("MM-dd-yyyy_H.mm.ss");
if (this.checkbox_zipfiles.Checked == true)
{
System.IO.Compression.ZipFile.CreateFromDirectory(sourcedir.FullName, targetdir.FullName + @"\BACKUP_" + sourcedir.Name + @"_" + dateString + @".zip");
LogBackup();
}
else
{
foreach (var file in sourcedir.GetFiles()
.Where(f => !extensionsToSkip.Contains(f.Extension) && !filesToSkip.Contains(f.FullName)).ToList())
{
file.CopyTo(targetdir.FullName + @"\" + file.Name, true);
LogBackup();
}
}
}
}
到目前为止,我的代码只适用于文件,如何使其适用于文件夹和子文件夹以及文件夹中的文件?
答案 0 :(得分:0)
您可以编写像这样的复制递归函数(伪代码)
public void CopyData(string sourceDirectoryPath, string desDirectoryPath)
{
foreach File in sourceDirectoryPath
{
copy to desDirectoryPath
}
foreach currentDirectory in sourceDirectoryPath
{
// recursive function
CopyData( currentDirectory, correspondingdesDirectoryPath)
}
}
答案 1 :(得分:0)
我没有测试代码,但它看起来像这样:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace divideSquare
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Backup_Click(object sender, EventArgs e)
{
List<DirectoryInfo> SourceDir = this.lbox_Sources.Items.Cast<DirectoryInfo>().ToList();
List<DirectoryInfo> TargetDir = this.lbox_Targets.Items.Cast<DirectoryInfo>().ToList();
for(int i = 0; i < SourceDir.Count(); i++)
{
DirectoryInfo sourcedir = SourceDir[i];
DirectoryInfo targetdir = TargetDir[i];
RecursiveZip(sourcedir, targetdir);
}
}
private void RecursiveZip(DirectoryInfo sourcedir, DirectoryInfo targetdir)
{
string dateString = DateTime.Now.ToString("MM-dd-yyyy_H.mm.ss");
if (this.checkbox_zipfiles.Checked == true)
{
System.IO.Compression.ZipFile.CreateFromDirectory(sourcedir, targetdir.FullName + @"\BACKUP_" + sourcedir.Name + @"_" + dateString + @".zip");
LogBackup();
}
else
{
foreach (var file in sourcedir.GetFiles()
.Where(f => !extensionsToSkip.Contains(f.Extension) && !filesToSkip.Contains(f.FullName)).ToList())
{
file.CopyTo(targetdir.FullName + @"\" + file.Name, true);
LogBackup();
}
foreach(DirectoryInfo folder in sourcedir.GetDirectories())
{
RecursiveZip(folder, new DirectoryInfo(targetdir.FullName + "\\" + folder.Name));
}
}
}
}