Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! Friend i want to copy a folder with it's file and subfolder to destination folder.Help me out ..
actualy i am copying the source folder wtih it's file to destination but only file is copied i could not copied folder wtih file in destination folder ...i used these code ..help me...

C#
private static void CopyDirectory(string sourcePath, string destPath)
{
    if (!Directory.Exists(destPath))
    {
        Directory.CreateDirectory(destPath);
    }

    foreach (string file in Directory.GetFiles(sourcePath))
    {
        string dest = Path.Combine(destPath, Path.GetFileName(file));
        File.Copy(file, dest);
    }

    foreach (string folder in Directory.GetDirectories(sourcePath))
    {
        string dest = Path.Combine(destPath, Path.GetFileName(folder));
        CopyDirectory(folder, dest);
    }
}
Posted
Updated 28-Jun-12 10:11am
v3

I think in your second loop you want to replace
C#
string dest = Path.Combine(destPath, Path.GetFileName(folder));

with
C#
string dest = Path.Combine(destPath, Path.GetDirectoryName(folder));
 
Share this answer
 
Comments
Clifford Nelson 28-Jun-12 18:29pm    
That definately does not work. You made an serious error:

sourcePath
"C:\\appl\\pimbeta\\tlc\\compliance\\ltalimits\\server\\config"
Path.GetDirectoryName(sourcePath)
"C:\\appl\\pimbeta\\tlc\\compliance\\ltalimits\\server"

from the immediate window
lewax00 28-Jun-12 18:56pm    
That's no good. Hmm I'll take a look when I get home...
It looks like your code is similar to what is here:

http://msdn.microsoft.com/en-us/library/bb762914.aspx[^]

I would recommend you go through that MSDN article and figure out why each piece is doing what it is doing. In the end, you could just copy and paste the code and it would work but it is much better for you if you actually learn why it is doing what it is doing.
 
Share this answer
 
v2
Comments
[no name] 28-Jun-12 16:35pm    
Your answer is good. Not sure why you got a 1.
Gave ya a 5 :)
Sergey Alexandrovich Kryukov 28-Jun-12 17:03pm    
My vote is 5, too. Screw the moron who voted 1.
--SA
You probably are having problems with the path. If you have a slash at the end, then you have a problem. The following seems to work:

private static void CopyDirectory(string sourcePath, string destPath)
{

  if (!Directory.Exists(destPath))
  {
    Directory.CreateDirectory(destPath);
  }
  var fixedSource = new DirectoryInfo(sourcePath);
  var fixedDest = new DirectoryInfo(destPath);

  foreach (var file in fixedSource.GetFiles())
  {
    string dest = Path.Combine(fixedDest.FullName, file.Name);
    file.CopyTo(dest);
  }

  foreach (var folder in fixedSource.GetDirectories())
  {
    string dest = Path.Combine(destPath, folder.Name);
    CopyDirectory(folder.FullName, dest);
  }
}


This is not the way I would recommend that you do it. I would have a non-recursive method that would fix any path issues, then your code will work, but you may have an issue with duplicates, and you need to deal with that also.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900