如何排除在复制过程中复制的特定文件。我想要排除复制,例如name.xml,adress.xml和data.xml
以下是我使用的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
答案 0 :(得分:1)
准备一个List(Of String),其中包含您不想复制的文件的名称,然后使用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