2011-04-02 105 views

回答

12

您需要遞歸遍歷所有文件和文件夾並複製它們。這種方法爲你做這項工作:

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 
+1

哇!這絕對有效!非常感謝你!!你保存了晚上;) – 2011-04-02 21:02:34

+0

不客氣:D – 2011-04-02 21:15:22

2

System.IO有,你可以在一個遞歸的方式使用做這一切從代碼兩班。

  • 的DirectoryInfo
  • 的FileInfo

DirectoryInfo中有兩個方法是相關的:

  • GetDirectories
  • 的GetFiles

FileInfo的有CopyTo方法

鑑於這些對象和方法以及一些創造性遞歸,您應該能夠相當容易地複製這些東西。

相關問題