简体   繁体   English

如何从C#中的openFileDialog复制选定的文件夹,子文件夹和文件

[英]How to copy selected folders, subfolders and files from openFileDialog in c#

I have a function that is suposed to copy all folders, subfolders files selected from openFileDialog from a location to another: 我有一个函数可以将所有文件夹,从openFileDialog选择的子文件夹文件从一个位置复制到另一个位置:

I've made this function to copy all the selected paths: 我已经制作了此函数来复制所有选定的路径:

 public void CopiarFicheiros(string CopyTo, List<string> FilesToCopy )
        {

            foreach (var item in FilesToCopy)
            {
                string DirectoryName = Path.GetDirectoryName(item);
                string Copy = Path.Combine(CopyTo, DirectoryName);
                if (Directory.Exists(Copy) && DirectoryName.ToLower() != "newclient" && DirectoryName.ToLower() != "newservice")
                {
                    Directory.CreateDirectory(Copy);
                    File.Copy(item, Copy + @"\" + Path.GetFileName(item), true);
                }
                else File.Copy(item, CopyTo + @"\" + Path.GetFileName(item), true);
            }

        }

The logic is full of flaws and I'm running out of time and can't seem to find a proper solution to this. 逻辑充满了缺陷,我的时间不多了,似乎无法找到适当的解决方案。

This is how I get the selected files and folders from the dialog: 这是我从对话框中获取所选文件和文件夹的方式:

  private List<string> GetFiles()
        {
            var Files = new List<string>();
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string sFileName = openFileDialog.FileName;
                string[] arrAllFiles = openFileDialog.FileNames;
                Files = arrAllFiles.ToList();
            }
            return Files;
        }

Does anyone have a better solution or a clue to what I need to change to successfully do this? 有没有人有更好的解决方案或我需要更改以成功完成此操作的线索? Any help is highly appreciated, thank you 任何帮助表示高度赞赏,谢谢

Don't use OpenFileDialog to choose the folder. 不要使用OpenFileDialog选择文件夹。 As the name suggests, it not made for that task. 顾名思义,它不是完成该任务的。 You want the FolderBrowserDialog class for this task. 您需要此任务的FolderBrowserDialog

How to: Copy Directories : 如何:复制目录

// Copy from the current directory, include subdirectories.
DirectoryCopy(".", @".\temp", true);

private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
    // Get the subdirectories for the specified directory.
    DirectoryInfo dir = new DirectoryInfo(sourceDirName);

    if (!dir.Exists)
    {
        throw new DirectoryNotFoundException(
            "Source directory does not exist or could not be found: "
            + sourceDirName);
    }

    DirectoryInfo[] dirs = dir.GetDirectories();
    // If the destination directory doesn't exist, create it.
    if (!Directory.Exists(destDirName))
    {
        Directory.CreateDirectory(destDirName);
    }

    // Get the files in the directory and copy them to the new location.
    FileInfo[] files = dir.GetFiles();
    foreach (FileInfo file in files)
    {
        string temppath = Path.Combine(destDirName, file.Name);
        file.CopyTo(temppath, false);
    }

    // If copying subdirectories, copy them and their contents to new location.
    if (copySubDirs)
    {
        foreach (DirectoryInfo subdir in dirs)
        {
            string temppath = Path.Combine(destDirName, subdir.Name);
            DirectoryCopy(subdir.FullName, temppath, copySubDirs);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# - 如何自定义OpenFileDialog以选择多个文件夹和文件? - C# - How to customize OpenFileDialog to select multiple folders and files? 在C#中从OpenFileDialog对选定文件运行CMD命令 - Running CMD command on selected files from OpenFileDialog in C# 从OpenFileDialog复制所选文件c# - Copying Selected file from OpenFileDialog c# 如何在子文件夹中组织C#源文件? - How to organise C# source files in subfolders? 将文件从一台服务器上的一组文件夹复制到另一台服务器上的同一组文件夹 C# - Copy files from a set of folders on one server to same set of folders on another server C# 如何在C#中不使用openfileDialog的情况下选择多个文件 - how to select multiple files without using openfileDialog in c# 如何禁用上下文菜单表单 OpenFileDialog、CommonOpenFileDialog。或者如何在 c# 中禁用 OpenFileDialog 中的删除菜单 - How to disable context menu form OpenFileDialog,CommonOpenFileDialog.Or how to disable delete menu from OpenFileDialog in c# OpenFileDialog:如何在本地文件夹中复制文件? - OpenFileDialog: How to copy files in a local folder? C#Windows窗体-使用MultiSelect,OpenFileDialog和FileBrowserDialog选择和复制多个文件 - C# Windows Forms - Select and Copy Multiple Files w MultiSelect, OpenFileDialog, & FileBrowserDialog 打开多个文件(OpenFileDialog,C#) - Opening multiple files (OpenFileDialog, C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM