VB 2010:如何将一个文件夹的所有子文件夹复制到另一个文件夹中? [英] VB 2010: How to copy all subfolders of a folder in an other folder?

查看:368
本文介绍了VB 2010:如何将一个文件夹的所有子文件夹复制到另一个文件夹中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Basic 2010中遇到一个问题:如何将所有子文件夹(仅子文件夹而不是主文件夹)复制到另一个文件夹中?

I got a question in Visual Basic 2010: How can I copy all subfolders (only the subfolders, not the main folder) into another folder?

感谢

推荐答案

您需要递归遍历所有文件和文件夹并进行复制。此方法为您完成工作:

You need to recursivly iterat through all the files and folders and copy them. This method do the job for you:

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

这篇关于VB 2010:如何将一个文件夹的所有子文件夹复制到另一个文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆