-1

I want to copy all the content in a folder into two file destination folder.

   foreach (string newPath in Directory.GetFiles(@"E:\autotransfer", "*.*",
            SearchOption.AllDirectories))
            File.Copy(newPath, newPath.Replace(@"E:\autotransfer", 
   @"E:\autotransferbackup"), true);

   foreach (string newPath in Directory.GetFiles(@"E:\autotransfer", "*.*",
            SearchOption.AllDirectories))
            File.Copy(newPath, newPath.Replace(@"E:\autotransfer", 
   @"E:\autotransferbackupcp"), true); 
Sam
  • 15
  • 2
  • 11
  • 4
    Please be more specific. Other than the fact that using `string.Replace()` to manipulate file paths is not a great idea, the little bit of code you posted seems like it would work. Or at least do _something_. You say it _"doesn't work"_. In what way, _specifically_, does the code not work? Provide a good [mcve] that reliably reproduces your problem, and explain _precisely_ what that code does, and what you want it to do instead. (Note that the casing of the path returned by `GetFiles()` might not match the casing in your `Replace()` call.) – Peter Duniho May 27 '17 at 07:38

2 Answers2

0

You can use this code, for more information see the answer here: Copy all files in directory

    void Copy(string sourceDir, string targetDir)
    {
        Directory.CreateDirectory(targetDir);

        foreach (var file in Directory.GetFiles(sourceDir))
            File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)));

        foreach (var directory in Directory.GetDirectories(sourceDir))
            Copy(directory, Path.Combine(targetDir, Path.GetFileName(directory)));
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Copy("E:\autotransfer", "E:\autotransferbackup");
        Copy("E:\autotransfer", "E:\autotransferbackupcp");
    }

If the directory structure is not the same, then you will need to check if the folder exists, if not, create it first, then copy the files.

Tod
  • 160
  • 12
  • Thank you sir.It works.My next problem is how it will auto transfer in every time that the folder autotransfer have a content and it will automatically sent to two directory. For I am a beginner of programming I need some help. Thank you sir. – Sam May 27 '17 at 09:13
  • What if I want to copy only the content of the two sub folders in the certain directory, how would it be. – Sam Jun 07 '17 at 01:02
0

Taken from here on msdn: https://msdn.microsoft.com/en-us/library/bb762914(v=vs.110).aspx

You can copy this function and use it in your code.

Hope this helps.

using System;
using System.IO;

class DirectoryCopyExample
{
    static void Main()
    {
        // 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);
            }
        }
    }
}
TheNoob
  • 861
  • 2
  • 11
  • 25
  • Thank you for your response. I tried first the 1st suggestion/answer above and it works for me. Maybe for other situations I will try your code sir. Thank you sir. – Sam May 27 '17 at 09:15