2016-10-19 33 views
1

我想要复制一个特定的文件夹,它的内容使用vb.net,我发现所有的方法只复制指定文件夹的内容,但不是整个文件夹。我想这条道路通向得到充分而不仅仅是contents.I复制有眼前这个代码的文件夹:复制一个文件夹,它的内容在vb.net

Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory("C:\Users\Max\Desktop\test\" & sender.name, "C:\Users\Max\Desktop\test2") 
+4

https://msdn.microsoft.com/en-us/library/bb762914(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 – Neal

+0

仍然只能复制文件夹的内容,而不是文件夹本身+内容。除非我以某种方式错误地使用这个函数。 – Max

回答

0

你不能只是复制一个目录和它的所有内容与一个线的代码。但是,您可以“剪切和粘贴”目录有:

Directory.Move("C:\Users\Max\Desktop\test\" & sender.name, "C:\Users\Max\Desktop\test2\" & sender.name) 

要复制你需要在目标目录下创建同名的新文件夹,然后将内容复制到其中:

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) 
相关问题