将子目录复制到目录 [英] Copy Sub directories to directory
问题描述
引用此问题/代码:
我试图将一个buntch子目录复制到不同的目录。我想更新此代码:
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)
但我收到此错误:
System.IO.DirectoryNotFoundException:找不到
的一部分路径'C:\Temp \Summer2011\Newfolder\新文本Document.txt的复制。 at
System.IO._E_Error.WinIOError(Int32 errorCode,String maybeFullPath)
在System.IO.File.InternalCopy(String sourceFileName,String
destFileName,Boolean覆盖)在System.IO。 File.Copy(String
sourceFileName,String destFileName)... etc
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
有一个更简单的方法,使用更少的代码行,如fso As System。对象版本?此外,我有System.IO导入,但File.Copy和Directory.GetFiles不是蓝色,这可以是问题吗?
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.
推荐答案
尝试使用 Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory 方法。
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(sourceDirectory, destinationDirectory)
希望这有帮助。
这篇关于将子目录复制到目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!