2013-05-19 51 views
0

如何在複製過程中排除正在複製的特定文件。我想從被複制例如name.xml的,adress.xml和data.xml中vb.net從複製中排除特定的文件名?

這裏排除是從MSDN代碼我使用:

Dim BackupDir As String = Application.StartupPath & "\backup" 
    Dim sourceDir As String = Application.StartupPath 

    If Not Directory.Exists(BackupDir) Then 
     IO.Directory.CreateDirectory(BackupDir) 
    End If 

    Try 
     Dim xmlList As String() = Directory.GetFiles(sourceDir, "*.xml") 

     For Each f As String In xmlList 
      'Remove path from the file name. 
      Dim fName As String = f.Substring(sourceDir.Length + 1) 
      File.Copy(Path.Combine(sourceDir, fName), Path.Combine(BackupDir, fName), True) 
     Next 
    Catch copyError As IOException 
     Console.WriteLine(copyError.Message) 
    End Try 

回答

1

準備列表(串)與名您不想複製的文件,然後使用Path.GetFileName從Directory.GetFiles()返回的完整文件名中提取文件名。 如果該文件包含在excludedFiles

Dim excludeFiles = new List(Of String)() 
    excludedFiles.Add("file1.xml") 
    excludedFiles.Add("file2.xml") 
    excludedFiles.Add("file3.xml") 


    For Each f As String In xmlList 
     'Remove path from the file name. 
     Dim fName As String = Path.GetFileName(f) 
      if excludedFiles.IndexOf("file3.xml", _ 
       StringComparison.CurrentCultureIgnoreCase) <> -1 Then 
      File.Copy(f, Path.Combine(BackupDir, fName), True) 
     End If 
    Next 
+0

史蒂夫列表執行復制前檢查的工作就像一個魅力,謝謝。剛剛將d字母添加到第一個excludeFiles中,並將c添加到第二個中:) – Chelovek

+0

更改了IndexOf中的方法Contains。這允許傳遞枚舉來避免文件名大小寫(I.E. FILE1.xml) – Steve

相關問題