我想在C#中将目录的全部内容从一个位置复制到另一个位置.
似乎没有办法使用System.IO没有大量递归的类来完成此操作.
VB中有一个方法,如果我们添加一个引用,我们就可以使用它Microsoft.VisualBasic:
new Microsoft.VisualBasic.Devices.Computer().
FileSystem.CopyDirectory( sourceFolder, outputFolder );
Run Code Online (Sandbox Code Playgroud)
这似乎是一个相当丑陋的黑客.有没有更好的办法?
引用此问题/代码:
我正在尝试将一小撮子目录复制到另一个目录.我想要更新此代码:
Dim fso As System.Object = New System.Object
fso = CreateObject("scripting.filesystemobject")
fso.copyfolder(sour, dest)
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误:
System.IO.DirectoryNotFoundException:找不到路径'C:\ Temp\Summer2011\Newfolder\Copy of New Text Document.txt'的一部分.System.IO.File.Copy.Copy(String sourceFileName,String destFileName)的System.IO.File.InternalCopy(String sourceFileName,String destFileName,Boolean overwrite)中的System.IO._E_Error.WinIOError(Int32 errorCode,String maybeFullPath). .等等
有了这个.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, …Run Code Online (Sandbox Code Playgroud)