[C#]Copying specific folder using c#

07/18/2016 11:06 mauripan#1
Hi there fellow programmers i'm having trouble figuring out how to copy 3 specific folders into one directory.
1 this codes cant copy directory
================================================== ==
DirectoryInfo Fold1 = new DirectoryInfo(@"C:\FolderB\201607061");
DirectoryInfo Fold2 = new DirectoryInfo(@"C:\FolderB\201607062");

private void btnPhotosCopy_Click(object sender, EventArgs e)
{
Fold1.CopyTo(@"E:\Photos\201607061");
Fold1.CopyTo(@"E:\Photos\201607062");
}
================================================== ==

2 this code can copy directory but only one
================================================== ==
public partial class mainForm : Form
{
const string FromDir = "C:/FolderA/";
const string FromDir2 = "C:/FolderB/";
const string ToDir = "D:/USBTest/";

public mainForm()
{
InitializeComponent();
}

private void btnCopy_Click(object sender, EventArgs e)
{


DirectoryInfo dirCopyFrom = new DirectoryInfo(FromDir2);
FileInfo[] diskFiles = dirCopyFrom.GetFiles(FromDir2);
foreach (FileInfo curFile in diskFiles) <--- I cant make 2 foreach loop
{
try
{
File.Copy(curFile.FullName, ToDir + curFile.Name, true);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}
================================================== ==
Please help:handsdown:
07/18/2016 15:05 YatoDev#2
why not

Code:
using System.IO;

foreach(string file in Directory.GetFiles(path)
{
      File.Copy(file, Path.Combine(newPath, Path.GetFileName(file)));
}
and this for each directory?
07/18/2016 19:34 mauripan#3
uhh... I still didn't figure it out. My codes are not working. Still I cant copy FolderA in drive:C to drive: D
07/18/2016 19:40 .Scy#4
[Only registered and activated users can see links. Click Here To Register...]

microsoft can explain that better than i can.

that should be what you want.
07/19/2016 10:36 mauripan#5
Hi Master .Scy can you give example on copying the whole folder from drive c: to drive d: please....
07/19/2016 13:01 .Scy#6
This is the code that [Only registered and activated users can see links. Click Here To Register...] site provides.
Code:
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);
            }
        }
    }
}
if you have any knowledge of c# you will know how to call that function with the right parameters, no need for me to write your code for you.

this code-snippet even offers an example of use