【发布时间】:2011-11-03 17:39:21
【问题描述】:
参考这个问题/代码:
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\Copy of New Text Document.txt”。在 System.IO._E_Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.File.InternalCopy(字符串源文件名,字符串 System.IO.File.Copy(String) 处的 destFileName,布尔覆盖 sourceFileName, String destFileName)...等
有了这个 .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 不是蓝色的,这可能是问题吗?我已加载系统作为参考。
谢谢!
【问题讨论】:
-
那个目录路径存在吗?
标签: vb.net visual-studio-2010 directory copy