vb.net如果该文件夹不存在,如何通过创建文件夹将文件从一个目录复制到另一个目录 [英] vb.net how to copy file from one Directory to another directory by create the folder if that folder is not exists

查看:100
本文介绍了vb.net如果该文件夹不存在,如何通过创建文件夹将文件从一个目录复制到另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

示例:

我在测试文件夹中有很多文件,但我只想复制1.txt

源路径:C:\\ \\ temp\test\1.txt

目的地路径:C:\ data \



如果C:\ Data \不包含temp或test文件夹,它应该在复制1.txt之前创建文件夹。



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



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





 私人  Sub  btnBackup_Click( ByVal  sender  As 系统。对象 ByVal  e  As  System.EventArgs)句柄 btnBackup.Click 
Dim sourcepath 作为 字符串 = C:\ temp\test\1.txt
Dim DestPath As 字符串 = C:\ Data \\ \\
CopyDirectory(sourcepath,DestPath)

结束 Sub





 私有 共享  Sub  CopyDirectory(sourcePath  As   String ,destPath  As  字符串
如果 Directory.Exists(destPath)然后
Directory.CreateDirectory(destPath)
结束 如果

对于 每个 file__1 作为 字符串 目录中.GetFiles(sourcePath)
Dim des t As String = Path.Combine(destPath,Path.GetFileName(file__1))
File .Copy(file _1,dest)
下一步

用于 < span class =code-keyword>每个文件夹作为 字符串 Directory.GetDirectories(sourcePath)
Dim dest As 字符串 = Path.Combine(destPath,Path.GetFileName(文件夹))
CopyDirectory(folder,dest)
下一步
结束 Sub

解决方案

这应该工作



  Imports  System.IO 

公开 Form1

私有 Sub Button1_Click( ByVal sender As System。 Object ByVal e As System.EventArgs)句柄 Button1.Click

Dim sourcepath 作为 字符串 = E:\ temp\test\1.txt
Dim DestPath 正如 字符串 = E:\\ \\ data\
CopyDirectory(sourcepath,De stPath)

结束 Sub









 私人 共享  Sub  CopyDirectory( ByVal  sourcePath 作为 字符串 ByVal  destPath 作为 字符串
如果 Directory.Exists(destPath)然后
Directory.CreateDirectory(destPath)
结束 如果

用于 每个 file__1 作为 字符串 Directory.GetFiles(Path.GetDirectoryName(sourcePath))
Dim dest As String = Path.Combine(destPath,Path。 GetFileName(file _1))
File.Copy(file _1,dest)
Next

对于 每个文件夹作为 字符串 Directory.GetDirectories(Path.GetDirectoryName(sourcePath))
Dim dest As String = Path.Combine(destPath,Path.GetFileName(folder))
CopyDirectory (folder,dest)
下一步
结束 Sub
结束





我更改了Directory.GetDirectories(sourcePath)



to



Directory.GetDirectories(Path.GetDirectoryName(sourcePath))





希望这有帮助,如果是,那么接受并投票给答案,否则回复你的疑问

--Rahul D。


我对解决方案1有一个重要的补充。



检查目录是否已存在绝对是多余的。您只需要无条件地创建目录。如果目录已经存在, Directory.CreateDirectory 无论如何都能正常工作。



-SA

I have some problem with copying the files from one Directory to another directory by create the folder if that folder is not exists in destination directory.
Example:
I have a lot of files in test folder but i only want to copy 1.txt
Source path: C:\temp\test\1.txt
destination path: C:\Data\

if C:\Data\ doesn't contains "temp" or "test" folder, it should create the folder before coping the 1.txt.

Copied to 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\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

解决方案

This should work

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim sourcepath As String = "E:\temp\test\1.txt"
        Dim DestPath As String = "E:\Data\"
        CopyDirectory(sourcepath, DestPath)

    End Sub





    Private Shared Sub CopyDirectory(ByVal sourcePath As String, ByVal destPath As String)
        If Not Directory.Exists(destPath) Then
            Directory.CreateDirectory(destPath)
        End If

        For Each file__1 As String In Directory.GetFiles(Path.GetDirectoryName(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(Path.GetDirectoryName(sourcePath))
            Dim dest As String = Path.Combine(destPath, Path.GetFileName(folder))
            CopyDirectory(folder, dest)
        Next
    End Sub
End Class



I have changed "Directory.GetDirectories(sourcePath)"

to

"Directory.GetDirectories(Path.GetDirectoryName(sourcePath))"


Hope this helps if yes then accept and vote the answer otherwise revert back with your queries
--Rahul D.


I have one important addition to Solution 1.

Checking if a directory already exists is absolutely redundant. You just need to create a directory unconditionally. If a directory already exist, Directory.CreateDirectory will work correctly anyway.

—SA


这篇关于vb.net如果该文件夹不存在,如何通过创建文件夹将文件从一个目录复制到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆