如何将文件夹复制到可选位置 [英] how to copy folders to optional location

查看:93
本文介绍了如何将文件夹复制到可选位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,用于将文件夹从一个位置复制到另一个位置.

代码如下所示:

I have a button I use to copy a folder from one location to another.

the code looks like this:

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Const FOLDER_FROM As String = "C:\Testcopy1" ' Folder to copy from 
        Const FOLDER_TO As String = "C:\Mappestruktur" 'Folder to create above folder in 
        Dim newDir As DirectoryInfo = Directory.CreateDirectory(FOLDER_TO & "\" & New DirectoryInfo(FOLDER_FROM).Name)
        CopyFiles(New DirectoryInfo(FOLDER_FROM), newDir)
    End Sub

    Private Sub CopyFiles(ByVal FolderFrom As DirectoryInfo, ByVal FolderTo As DirectoryInfo)
        Dim Files() As String = Directory.GetFiles(FolderFrom.FullName)
        Dim SF() As String = Directory.GetDirectories(FolderFrom.FullName)
        Dim Var As Integer

        For Var = Files.GetLowerBound(0) To Files.GetUpperBound(0)
            File.Copy(Files(Var), FolderTo.FullName & "\" & New FileInfo(Files(Var)).Name)
        Next

        For Var = SF.GetLowerBound(0) To SF.GetUpperBound(0)
            Dim dName As String = New DirectoryInfo(SF(Var)).Name
            Dim newD As New DirectoryInfo(FolderTo.FullName & "\" & dName)
            newD.Create()
            CopyFiles(New DirectoryInfo(FolderFrom.FullName & "\" & dName), newD)
        Next

    End Sub



现在,我的项目中有一个文件夹,我想将其复制到可选位置.
如何将"Folder from"常量设置为项目内的某个位置,以及如何在运行时设置将文件夹复制到的可选位置?



Now I have a folder inside my project I would like to copy to an optional location.
how do I set the "Folder from" const to a location inside my project and how can I set an optional location to copy the folder to at runtime?

推荐答案

首先总而言之,您正在谈论运行时.在运行时,没有项目"之类的东西,没有在我的项目内部"之类的东西.问题很简单:您有一个目录,该目录由其路径名和新位置来引用.您的所有代码也都不需要. 仅通过一次调用即可完成:对静态方法System.IO.Directory.Move的调用,仅此而已.

请参阅 http://msdn.microsoft.com/en-us/library/system .io.directory.move.aspx [ ^ ].

—SA
First of all, you are talking about run-time. During run-time, there is no such thing as "project", there are no "inside my project" and anything like that. The problem is simple: you have a directory referenced by its path name and a new location for it. All your code is also not needed. It''s done by only one call: to the static method System.IO.Directory.Move, nothing else.

See http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx[^].

—SA


这篇关于如何将文件夹复制到可选位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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