2017-04-20 104 views
0
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(); 
        } 
       } 
      } 
     } 

到目前爲止,我的代碼只適用於文件,如何使它適用於文件夾和子文件夾以及文件夾中的文件?只複製某些文件和文件夾的程序C#

+1

寫拷貝遞歸函數 – TriV

+0

[在C#中複製目錄的全部內容]的可能副本(http://stackoverflow.com/questions/58744/copy-the-entire-contents-of-a-directory-in-c-sharp) – PaulF

回答

0

你可以寫這樣的複製遞歸函數(僞代碼)

public void CopyData(string sourceDirectoryPath, string desDirectoryPath) 
{ 
    foreach File in sourceDirectoryPath 
    { 
     copy to desDirectoryPath 
    } 

    foreach currentDirectory in sourceDirectoryPath 
    { 
     // recursive function 
     CopyData(currentDirectory, correspondingdesDirectoryPath) 
    } 
} 
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)); 
        } 
       } 
     } 

} 
相關問題