Vb.net 将子文件夹中的所有文件复制到新文件夹中

Vb.net 将子文件夹中的所有文件复制到新文件夹中,vb.net,file-copying,Vb.net,File Copying,我一直在网上搜索,找到了很多关于复制文件的帖子,但是我没有在子文件夹中复制文件。我要做的是给出一个sourcePath和一个destinationPath。所有文件(包括子文件夹中的文件)都将被复制到DestinationOpath中。我已经修改了很多代码,但是我还不能让搜索子文件夹工作 我尝试过的代码:但是在文件复制行的“dest”上给了我一个错误 Public Sub CopyAllFiles(ByVal sourcePath As String, ByVal destPath As Str

我一直在网上搜索,找到了很多关于复制文件的帖子,但是我没有在子文件夹中复制文件。我要做的是给出一个sourcePath和一个destinationPath。所有文件(包括子文件夹中的文件)都将被复制到DestinationOpath中。我已经修改了很多代码,但是我还不能让搜索子文件夹工作

我尝试过的代码:但是在文件复制行的“dest”上给了我一个错误

Public Sub CopyAllFiles(ByVal sourcePath As String, ByVal destPath As String)
    Dim files() As String = IO.Directory.GetFiles(destPath)

    For Each file As String In files
        ' Do work, example            
        Dim dest As String = Path.Combine(destPath, Path.GetFileName(file))
        file.Copy(file, dest, True)  ' Added True here to force the an overwrite 
    Next
End Sub
我尝试过的代码,但它会将子文件夹移到designationpath

    Public Sub CopyDirectory(ByVal sourcePath As String, ByVal destinationPath As String)
    Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)

    ' If the destination folder don't exist then create it
    If Not System.IO.Directory.Exists(destinationPath) Then
        System.IO.Directory.CreateDirectory(destinationPath)
    End If

    Dim fileSystemInfo As System.IO.FileSystemInfo
    For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
        Dim destinationFileName As String =
        System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)

        ' Now check whether its a file or a folder and take action accordingly
        If TypeOf fileSystemInfo Is System.IO.FileInfo Then
            System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
        Else
            ' Recursively call the mothod to copy all the neste folders
            CopyDirectory(fileSystemInfo.FullName, destinationFileName)
        End If
    Next
End Sub
我也尝试过这段代码,但它没有提供子文件夹中的文件

Private Function CopyDirectory(sourcedir As String, targetdir As String, overwrite As Boolean) As List(Of String)
    Dim failedCopy As List(Of String) = New List(Of String)

    Directory.CreateDirectory(targetdir)

    Dim files = Directory.GetFiles(sourcedir, "*.*", SearchOption.AllDirectories)
    For Each file In files
        Dim newfile = file.Replace(sourcedir, targetdir)
        Dim fi = New FileInfo(file)
        Try
            fi.CopyTo(newfile, overwrite)
        Catch ex As Exception
            failedCopy.Add(file)
        End Try

    Next
    Return failedCopy
End Function

这会让你很接近

Private Sub DirTestCopyButton_Click(sender As Object, e As EventArgs) Handles DirTestCopyButton.Click

    Try
        CopyDirectoryContents("c:\temp\", "c:\out")
        MessageBox.Show("Copy complete")
    Catch ex As Exception
        MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
    End Try

End Sub

Private Sub CopyDirectoryContents(sourcePath As String, destinationPath As String)

    If Not Directory.Exists(sourcePath) Then
        Return
    End If

    If Not Directory.Exists(destinationPath) Then
        Directory.CreateDirectory(destinationPath)
    End If

    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 File.Exists(newFilePath) Then
            'do something about this 
        Else
            File.Copy(filePathString, newFilePath)
        End If

    Next

End Sub

这会让你很接近

Private Sub DirTestCopyButton_Click(sender As Object, e As EventArgs) Handles DirTestCopyButton.Click

    Try
        CopyDirectoryContents("c:\temp\", "c:\out")
        MessageBox.Show("Copy complete")
    Catch ex As Exception
        MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
    End Try

End Sub

Private Sub CopyDirectoryContents(sourcePath As String, destinationPath As String)

    If Not Directory.Exists(sourcePath) Then
        Return
    End If

    If Not Directory.Exists(destinationPath) Then
        Directory.CreateDirectory(destinationPath)
    End If

    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 File.Exists(newFilePath) Then
            'do something about this 
        Else
            File.Copy(filePathString, newFilePath)
        End If

    Next

End Sub

这对你有帮助吗@AndrewMortimer否它将整个子文件夹复制到新位置。我想将子文件夹和主文件夹中的所有文件放入新文件夹location@MaxineHammett如果两个同名文件位于两个不同的子文件夹中,您将如何处理这种情况?@djv这是一个非常好的问题,也是问题之一。我希望子文件夹中的图形覆盖主文件夹中的图形这对您有帮助吗@AndrewMortimer否它将整个子文件夹复制到新位置。我想将子文件夹和主文件夹中的所有文件放入新文件夹location@MaxineHammett如果两个同名文件位于两个不同的子文件夹中,您将如何处理这种情况?@djv这是一个非常好的问题,也是问题之一。我希望子文件夹中的图形覆盖主文件夹中的图形谢谢Andrew。太棒了!谢谢你,安德鲁。太棒了!