从复制中排除文件夹

时间:2019-12-09 20:58:29

标签: vb.net file copy

嗨,我知道如何将文件从一个文件夹复制到另一个文件夹,但是当我们需要排除一个文件夹时,我遇到了麻烦。更具体地说,是“ .svn”文件夹。我尝试使用Not SourceDir.Name = skipDir,但这似乎没有什么不同。

这是我的代码。谢谢您的帮助。

    Private Sub CopyDirectoryContents(sourcePath As String, destinationPath As String)
    Dim SourceDir As DirectoryInfo = New DirectoryInfo(sourcePath)
    Dim DestDir As DirectoryInfo = New DirectoryInfo(destinationPath)
    Dim skipDir As String = ".svn"

    Cursor = Cursors.WaitCursor

    Try
        If Not Directory.Exists(sourcePath) Then
            MsgBox("The directory path entered does not exist. Please re-enter your source directory path", MsgBoxStyle.Exclamation, Title:="Missing source directory")
            Return
            txtSubfolder.Focus()
        End If

        If Not Directory.Exists(destinationPath) Then
            MsgBox("The destination directory does not exist. Creating folder for it.", MsgBoxStyle.Exclamation, Title:="Missing folder")
            Directory.CreateDirectory(destinationPath)
        End If

        Dim totalFiles As Integer = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories).Count
        Dim fileCount As Integer = 1

        For Each filePathString As String In Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories)
            Dim fileInfoItem As New FileInfo(filePathString)
            Dim newFilePath As String = Path.Combine(destinationPath, fileInfoItem.Name)

            If SourceDir.Exists And Not SourceDir.Name = skipDir Then
                If File.Exists(newFilePath) Then
                    Dim result As Integer = MsgBox("File already exists in destination folder. Do you want to overwrite it?", MsgBoxStyle.YesNo, Title:="File already in folder")
                    If result = DialogResult.No Then
                        Continue For
                    ElseIf result = DialogResult.Yes Then
                        statusText = "Replacing found image"
                        BackgroundWorker1.ReportProgress(count, statusText)
                        File.Copy(filePathString, newFilePath, True)
                    End If
                Else
                    File.Copy(filePathString, newFilePath)
                End If
            End If
            fileCount = +1
        Next
    Catch ex As Exception
        MsgBox("An error has occured. Please contact the system administrator. Exception: " & ex.Message)
    End Try
End Sub

0 个答案:

没有答案