在 vb.net 中复制文件夹及其内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40130549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying a folder and it's contents in vb.net
提问by Max
I want to copy a specific folder and it's contents using vb.net, the methods I found all just copy the contents of the specified folder but not the folder as a whole. I want the folder that the path leads to to be copied fully and not only the contents.I have this code at the moment:
我想使用 vb.net 复制特定文件夹及其内容,我发现的所有方法都只是复制指定文件夹的内容,而不是整个文件夹。我希望路径导致的文件夹被完全复制,而不仅仅是内容。我现在有这个代码:
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory("C:\Users\Max\Desktop\test\" & sender.name, "C:\Users\Max\Desktop\test2")
回答by Neal
You can't just copy a directory and all of it's contents with one line of code. You can however "cut and paste" a directory with:
您不能只用一行代码复制一个目录及其所有内容。但是,您可以使用以下命令“剪切和粘贴”目录:
Directory.Move("C:\Users\Max\Desktop\test\" & sender.name, "C:\Users\Max\Desktop\test2\" & sender.name)
To copy you'll need to create a new folder of the same name in the destination directory then copy the contents into it:
要复制,您需要在目标目录中创建一个同名的新文件夹,然后将内容复制到其中:
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)

