我有以下代码将所有内容复制到正确的位置,文件1,文件2和文件3除外。
来源 :“C:\ dir” 目标 :“C:\ tmp \ Backup”
Public Sub CopyDirectory(source As DirectoryInfo, target As DirectoryInfo) 'Recursively call the DeepCopy Method for each Directory For Each MyDir As DirectoryInfo In source.GetDirectories() CopyDirectory(MyDir, target.CreateSubdirectory(projectName & "\" & MyDir.Name)) Next ' Go ahead and copy each file in "source" to the "target" directory For Each MyFile As FileInfo In source.GetFiles() If Not (File.Exists(Path.Combine(target.FullName, MyFile.Name))) Then MyFile.CopyTo(Path.Combine(target.FullName, MyFile.Name)) Else MessageBox.Show("File already exist") End If Next End Sub文件1,2和3应该进入“Dir”文件夹,但它会一直显示在“备份”文件夹中。
C:\ |_ tmp |_ Backup |_ Dir |_ Folder 1 |_ File 11 |_ File 12 |_ File 13 |_ Folder 2 |_ File 21 |_ File 22 |_ File 23 |_ Folder 3 |_ File 31 |_ File 32 |_ File 33 |_ File 1 |_ File 2 |_ File 3如何确保将文件1,2和3复制到“Dir”文件夹中?
I have the following code which copies everything in to the right place except File 1, File 2 & File 3.
Source: "C:\dir" Target: "C:\tmp\Backup"
Public Sub CopyDirectory(source As DirectoryInfo, target As DirectoryInfo) 'Recursively call the DeepCopy Method for each Directory For Each MyDir As DirectoryInfo In source.GetDirectories() CopyDirectory(MyDir, target.CreateSubdirectory(projectName & "\" & MyDir.Name)) Next ' Go ahead and copy each file in "source" to the "target" directory For Each MyFile As FileInfo In source.GetFiles() If Not (File.Exists(Path.Combine(target.FullName, MyFile.Name))) Then MyFile.CopyTo(Path.Combine(target.FullName, MyFile.Name)) Else MessageBox.Show("File already exist") End If Next End SubFile 1, 2 & 3 should go inside the "Dir" folder, but it keeps showing up in the "Backup" folder.
C:\ |_ tmp |_ Backup |_ Dir |_ Folder 1 |_ File 11 |_ File 12 |_ File 13 |_ Folder 2 |_ File 21 |_ File 22 |_ File 23 |_ Folder 3 |_ File 31 |_ File 32 |_ File 33 |_ File 1 |_ File 2 |_ File 3How do I make sure that File 1, 2, and 3 gets copied inside the "Dir" folder?
最满意答案
我认为你的问题在于这一行:
CopyDirectory(MyDir, target.CreateSubdirectory(projectName & "\" & MyDir.Name))我认为你想在目标上创建新文件夹。 要做到这一点,你这样做:
CopyDirectory(MyDir, target.CreateSubdirectory(MyDir.Name))嗯,对。 错过了这一行:
来源:“C:\ dir”目标:“C:\ tmp \ Backup”
你的程序正在完成你告诉它要做的事情。 获取dir中的文件并将其置于备份中。 如果要更改,则需要在启动第一次递归时将目标设置为C:\ temp \ Backup \ dir。
I think your problem lies in this line:
CopyDirectory(MyDir, target.CreateSubdirectory(projectName & "\" & MyDir.Name))I take it you want to create the new folder at target. For that to happen you do this:
CopyDirectory(MyDir, target.CreateSubdirectory(MyDir.Name))Ehm right. Missed this line:
Source: "C:\dir" Target: "C:\tmp\Backup"
Your program is doing exactly what you are telling it to do. Take the files in dir and put it in backup. If you want that to change you need to set target to C:\temp\Backup\dir when you start the first recursion.
更多推荐
发布评论