Search code examples
c#filecopyopenfiledialog

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:

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


Solution

  • Don't use OpenFileDialog to choose the folder. As the name suggests, it not made for that task. You want the FolderBrowserDialog class for this task.

    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);
            }
        }
    }