VB.NET拷贝整个目录下所有子目录及文件的实例代

网络编程 2021-07-04 22:40www.168986.cn编程入门
这篇文章介绍了拷贝整个目录下所有子目录及文件的代码,有需要的朋友可以参考一下
代码如下:

 Public Sub CopyDerictory(ByVal DirectorySrc As DirectoryInfo, ByVal DirectoryDes As DirectoryInfo)
        Dim strDirectoryDesPath As String = DirectoryDes.FullName & "" & DirectorySrc.Name
        If Not Directory.Exists(strDirectoryDesPath) Then
            Directory.CreateDirectory(strDirectoryDesPath)
        End If
        Dim f, fs() As FileInfo
        fs = DirectorySrc.GetFiles()
        For Each f In fs
            File.Copy(f.FullName, strDirectoryDesPath & "" & f.Name, True)
        Next
        Dim DirSrc, Dirs() As DirectoryInfo
        Dirs = DirectorySrc.GetDirectories()
       // 递归调用自身
        For Each DirSrc In Dirs
            Dim DirDes As New DirectoryInfo(strDirectoryDesPath)
            CopyDerictory(DirSrc, DirDes)
        Next
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CopyDerictory(New DirectoryInfo("C:Documents and SettingsusernameFavorites"), New DirectoryInfo("g: emp"))
    End Sub

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by