.net 如果该文件夹不存在,如何通过创建文件夹将文件从一个目录复制到另一个目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9207257/
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
How to copy a file from one directory to another directory by creating the folder if that folder does not exist
提问by user1101157
I have some problem with copying the file from one directory to another directory by creating the folder if that folder does not exist in the destination directory.
如果目标目录中不存在该文件夹,则通过创建文件夹将文件从一个目录复制到另一个目录时遇到一些问题。
Example:
例子:
- Source path:
C:\temp\test\1.txt - Destination path:
C:\Data\
- 源路径:
C:\temp\test\1.txt - 目的地路径:
C:\Data\
If C:\Data\doesn't contain "temp" or "test" folder, it should create the folder before coping 1.txt.
如果C:\Data\不包含“temp”或“test”文件夹,则应在应对之前创建该文件夹1.txt。
Copied to C:\Data\temp\test\1.txt
复制到 C:\Data\temp\test\1.txt
Below is my code. But it doesn't work..
下面是我的代码。但它不起作用..
Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click
Dim sourcepath As String = "C:\temp\test.txt"
Dim DestPath As String = "C:\Data\"
CopyDirectory(sourcepath, DestPath)
End Sub
Private Shared Sub CopyDirectory(sourcePath As String, destPath As String)
If Not Directory.Exists(destPath) Then
Directory.CreateDirectory(destPath)
End If
For Each file__1 As String In Directory.GetFiles(sourcePath)
Dim dest As String = Path.Combine(destPath, Path.GetFileName(file__1))
File.Copy(file__1, dest)
Next
For Each folder As String In Directory.GetDirectories(sourcePath)
Dim dest As String = Path.Combine(destPath, Path.GetFileName(folder))
CopyDirectory(folder, dest)
Next
End Sub
回答by Tim Schmelter
The following is not a directory.
以下不是目录。
Dim sourcepath As String = "C:\temp\test.txt"
Because you are using it as a directory in Directory.GetFiles(sourcePath).
因为您将它用作Directory.GetFiles(sourcePath).
Apart from that, I recommend to elaborate your questions more the next time. The code raises meaningful exceptions like DirectoryNotFoundExceptionwith the appropriate path as message or (if the file exists) an IOExceptionwith message "The directory name is invalid". You should have added that to the question.
除此之外,我建议下次详细说明您的问题。该代码引发了有意义的异常,例如DirectoryNotFoundException将适当的路径作为消息或(如果文件存在)IOException带有消息"The directory name is invalid"。您应该将其添加到问题中。
So the solution simply is to remove the 1.txtfrom the directory-name:
所以解决方案只是1.txt从目录名中删除:
Dim sourcepath As String = "C:\temp\test\"
If you need to copy only one file, use CopyTo method:
如果您只需要复制一个文件,请使用CopyTo 方法:
Dim sourcepath As String = "C:\temp\test\"
Dim DestPath As String = "C:\temp\Data\"
If Not Directory.Exists(DestPath) Then
Directory.CreateDirectory(DestPath)
End If
Dim file = New FileInfo("C:\temp\test.txt")
file.CopyTo(Path.Combine(DestPath, file.Name), True)
回答by David McVey
Dim strMasterResourceDirectory As String
Dim strDirectory As String
strDirectory = "C:\TestDestination"
strMasterResourceDirectory = "TestResource"
If My.Computer.FileSystem.DirectoryExists(strDirectory) = False Then
My.Computer.FileSystem.CreateDirectory(strDirectory)
End If
' Loop through each file in the directory
For Each file As IO.FileInfo In New IO.DirectoryInfo(strDirectory).GetFiles
If file.Name <> "Thumbs.db" Then
System.IO.File.Delete(strDirectory & "\" & file.Name)
End If
Next
' Loop through each file in the directory
For Each file As IO.FileInfo In New IO.DirectoryInfo(strMasterResourceDirectory).GetFiles
If file.Name <> "Thumbs.db" Then
' copy resource to users local directory
file.CopyTo(strDirectory & "\" & file.Name)
End If
Next

