简体   繁体   中英

Copy Sub directories to directory

Referencing this question/code:

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

I'm trying to copy a buntch of sub directories to a different directory. I'm wanting to update this code:

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

    fso.copyfolder(sour, dest)

However I'm getting this error:

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

With this .NET version

 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

Is there a easier way of doing this with less lines of code like the fso As System.Object version? Also, I have System.IO imported however File.Copy and Directory.GetFiles are not colored blue, could this be the issue? I have the System loaded as a reference.

Thank you!

Try using the Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory method.

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

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM