将子目录复制到目录

时间:2011-08-25 13:58:54

标签: vb.net visual-studio-2010 directory copy

参考此问题/代码:

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

我正在尝试将一小撮子目录复制到另一个目录。我想更新这段代码:

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

    fso.copyfolder(sour, dest)

但是我收到了这个错误:

  

System.IO.DirectoryNotFoundException:找不到部分内容   路径'C:\ Temp \ Summer2011 \ Newfolder \ New Text of Document Text.txt'。在   System.IO._E_Error.WinIOError(Int32 errorCode,String maybeFullPath)   在System.IO.File.InternalCopy(String sourceFileName,String   System.IO.File.Copy(String,destFileName,Boolean overwrite)   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 As System.Object版本)是否有更简单的方法来执行此操作?另外,我导入了System.IO但是File.Copy和Directory.GetFiles没有蓝色,这可能是问题吗?我已将系统加载为参考。

谢谢!

1 个答案:

答案 0 :(得分:8)

尝试使用Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory方法。

Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(sourceDirectory, destinationDirectory)

希望这有帮助。