【问题标题】:How to copy a file from one directory to another directory by creating the folder if that folder does not exist如果该文件夹不存在,如何通过创建文件夹将文件从一个目录复制到另一个目录
【发布时间】:2012-03-01 17:03:39
【问题描述】:

如果目标目录中不存在该文件夹,我在通过创建文件夹将文件从一个目录复制到另一个目录时遇到一些问题。

例子:

  • 源路径:C:\temp\test\1.txt
  • 目标路径:C:\Data\

如果C:\Data\不包含“temp”或“test”文件夹,则应在处理1.txt之前创建该文件夹。

复制到C:\Data\temp\test\1.txt

下面是我的代码。但它不起作用..

Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click
          Dim sourcepath As String = "C:\temp\test\1.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

【问题讨论】:

  • 有什么错误吗?你使用的是 XP 还是 Windows 7
  • 在提交问题时,应该阻止诸如“它不起作用”之类的短语;)

标签: .net vb.net file directory copy


【解决方案1】:

以下不是目录。

Dim sourcepath As String = "C:\temp\test\1.txt"

因为您将其用作Directory.GetFiles(sourcePath) 中的目录。

除此之外,我建议下次再详细说明您的问题。代码会引发有意义的异常,例如 DirectoryNotFoundException 使用适当的路径作为消息或(如果文件存在)IOException 和消息 “目录名称无效”。您应该将其添加到问题中。

所以解决方案就是从目录名中删除1.txt

Dim sourcepath As String = "C:\temp\test\"

如果只需要复制一个文件,请使用CopyTo method:

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\1.txt")
file.CopyTo(Path.Combine(DestPath, file.Name), True)

【讨论】:

  • 我有很多文本文件,但我只想复制 1.txt 文件。
  • @user1101157:更新了我的答案。
【解决方案2】:
    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

【讨论】:

  • 欢迎来到 StackOverflow!请考虑为您的代码添加一些解释。谢谢!
猜你喜欢
  • 2015-05-05
  • 2013-05-21
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多