0

如何在复制过程中排除正在复制的特定文件。我想排除被复制,例如 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
4

1 回答 1

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
于 2013-05-19T19:28:07.417 回答