2011-08-25 212 views
0

引用這個問題/代碼:複製子目錄到目錄

How do I copy a folder and all subfolders and files in .NET?

我試圖子目錄的buntch複製到不同的目錄。我想更新此代碼:

Dim fso As System.Object = New System.Object 
    fso = CreateObject("scripting.filesystemobject") 

    fso.copyfolder(sour, dest) 

但是我得到這個錯誤:

System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Temp\Summer2011\Newfolder\Copy of New Text Document.txt'. at System.IO._E_Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite) at System.IO.File.Copy(String sourceFileName, String destFileName)...etc

有了這個.NET版本

Public Overrides Sub OnClick() 

      Dim sour As String = "C:\Temp243" 
      Dim dest As String = "C:\Temp\Summer2011\" 

      CopyDirectory(sour, dest) 

    End Sub 



    Private Sub CopyDirectory(ByVal SourcePath As String, ByVal DestPath As String) 

        If Directory.Exists(DestPath) Then 
       Directory.CreateDirectory(DestPath) 
      End If 

      For Each File As String In Directory.GetFiles(SourcePath) 
       Dim dest As String = IO.Path.Combine(DestPath, IO.Path.GetFileName(File)) 
       IO.File.Copy(File, dest) '<<<ERROR HERE 

      Next 

      For Each folder As String In Directory.GetDirectories(SourcePath) 
       Dim dd As String = IO.Path.Combine(DestPath, IO.Path.GetFileName(folder)) 
       CopyDirectory(folder, dd) 
      Next 

    End Sub 

是否有這樣做的一個簡單的方法這與像fso的System.Object版本一樣的代碼行少?此外,我有System.IO導入但是File.Copy和Directory.GetFiles不是藍色,這可能是問題嗎?我將系統加載爲參考。

謝謝!

+0

該目錄路徑是否存在? – NotMe

+0

@Chris Lively,是的。 – artwork21

回答