Copy Sub directories to directory

断了今生、忘了曾经 提交于 2020-01-17 07:22:05

问题


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!


回答1:


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

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

Hope this helps.



来源:https://stackoverflow.com/questions/7191677/copy-sub-directories-to-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!