我想使用 vb.net 复制特定文件夹及其内容,我发现的所有方法都只是复制指定文件夹的内容,而不是整个文件夹。我希望路径导致的文件夹被完全复制,而不仅仅是内容。我现在有这个代码:
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory("C:\Users\Max\Desktop\test\" & sender.name, "C:\Users\Max\Desktop\test2")
Run Code Online (Sandbox Code Playgroud)
您不能只用一行代码复制一个目录及其所有内容。但是,您可以使用以下命令“剪切和粘贴”目录:
Directory.Move("C:\Users\Max\Desktop\test\" & sender.name, "C:\Users\Max\Desktop\test2\" & sender.name)
Run Code Online (Sandbox Code Playgroud)
要复制,您需要在目标目录中创建一个同名的新文件夹,然后将内容复制到其中:
Dim SourcePath As String = "C:\Users\Max\Desktop\test\" & sender.name
Dim DestinationPath As String = "C:\Users\Max\Desktop\test2"
Dim newDirectory As String = System.IO.Path.Combine(DestinationPath, Path.GetFileName(Path.GetDirectoryName(SourcePath)))
If Not (Directory.Exists(newDirectory)) Then
Directory.CreateDirectory(newDirectory)
End If
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(SourcePath, newDirectory)
Run Code Online (Sandbox Code Playgroud)