3

I'm trying to copy one directory to another path.
I found this method, but it does not copy the directory, only the sub-directories and files inside it:

string sourcedirectory = Environment.ExpandEnvironmentVariables("%AppData%\\Program");

foreach (string dirPath in Directory.GetDirectories(sourcedirectory, "*", SearchOption.AllDirectories))
{
    Directory.CreateDirectory(dirPath.Replace(sourcedirectory, folderDialog.SelectedPath));
}
foreach (string newPath in Directory.GetFiles(sourcedirectory, "*.*", SearchOption.AllDirectories))
{
    File.Copy(newPath, newPath.Replace(sourcedirectory, folderDialog.SelectedPath), true);
}

How I can get the "Program" folder in output with all files and sub-folders?

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
DotNet
  • 59
  • 3
  • 9
  • What do you mean "it does not copy the directory"? It obviously copies the directory itself -- it wouldn't be able to copy the subdirectories and files if the top-level directory wasn't present in the output. Do you mean it doesn't copy the files in the top-level directory? It sure seems like it would, just scanning through the code. (Personally, I prefer a recursive solution, rather than relying on `string.Replace()` or other path manipulation, but the code here looks reasonable enough). – Peter Duniho Nov 07 '14 at 01:19
  • @PeterDuniho The directory "Program" is not created but the files and folders inside it are copied to destination. I need to get the "Program" directory in output. – DotNet Nov 07 '14 at 01:28
  • I believe it is duplicated question. Look at the MSDN solution of this question (I think it is last) - it should copy all, including the directory itself. [Copy directory c#](http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c-sharp) – Ziv Weissman Nov 07 '14 at 01:39
  • @DotNet: it should work fine, if you set `newPath` so that it's actually the output directory named `Program` you want the output to go to. Worst-case scenario, you just add `newPath = Path.Combine(newPath, Path.GetFileName(sourcedirectory))` to the code just before the first `foreach`. – Peter Duniho Nov 07 '14 at 01:41
  • @ZivWeissman This is not a duplicate question. I'm using the same method that is in one of the answers in the question you referred. – DotNet Nov 07 '14 at 01:46
  • @PeterDuniho This is very strange. I do not get the "Program" folder as output, just the folders and files inside it. You even tested? – DotNet Nov 07 '14 at 01:47
  • @DotNet: sorry, I should re-word the second suggestion: it's not `newPath` that needs changing, but rather `folderDialog.SelectedPath`. – Peter Duniho Nov 07 '14 at 01:54
  • @PeterDuniho Combining the paths worked, now I'm getting the "Program" folder as output with all their proper files and folders inside. Please add an answer and I will mark this as accepted. Thank you! – DotNet Nov 07 '14 at 01:58
  • done...sorry for the false start. :) – Peter Duniho Nov 07 '14 at 02:02

2 Answers2

1

If you adjust the output path before you start copying, it should work:

string sourcedirectory = Environment.ExpandEnvironmentVariables("%AppData%\\Program");

folderDialog.SelectedPath = Path.Combine(folderDialog.SelectedPath,
    Path.GetFileName(sourcedirectory));

foreach (string dirPath in Directory.GetDirectories(sourcedirectory, "*", SearchOption.AllDirectories))
{
    Directory.CreateDirectory(dirPath.Replace(sourcedirectory, folderDialog.SelectedPath));
}
foreach (string newPath in Directory.GetFiles(sourcedirectory, "*.*", SearchOption.AllDirectories))
{
    File.Copy(newPath, newPath.Replace(sourcedirectory, folderDialog.SelectedPath), true);
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
1

You can use a recursive function to do it:

private void button1_Click(object sender, EventArgs e)
    {
        this.CopyAll(new DirectoryInfo(@"D:\Original"), new DirectoryInfo(@"D:\Copy"));
    }
    private void CopyAll(DirectoryInfo oOriginal, DirectoryInfo oFinal)
    {
        foreach (DirectoryInfo oFolder in oOriginal.GetDirectories())
            this.CopyAll(oFolder, oFinal.CreateSubdirectory(oFolder.Name));

        foreach (FileInfo oFile in oOriginal.GetFiles())
            oFile.CopyTo(oFinal.FullName + @"\" + oFile.Name, true);
    }