Link to home
Start Free TrialLog in
Avatar of angus_young_acdc
angus_young_acdcFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Copy Files and Subfolders

Hey all,

I am trying to edit files from a set directory.  The directory will be similar to:
C:\MyDir\FolderName1
C:\MyDir\FolderName2
etc

Basically what I need to do is copy a selected folder to a new directory including all contents and subdirectories.  Currently I can copy individual files, but I can't seem to copy any folders (and their contents) that are subdirectories.

Can anybody help?
SOLUTION
Avatar of hongjun
hongjun
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try this

 DirectoryInfo dirInfo = new DirectoryInfo("sourcepath");
        dirInfo.MoveTo("destDirName");
Avatar of angus_young_acdc

ASKER

Hi hongjun,

Thanks for the quick response.  Is there anyway to get around this stage:
  if (Directory.Exists(element))
                    CopyDirectory(element, dst + Path.GetFileName(element));

Only reason I ask is that I am preforming this inside a method which takes multiple different parameters and would be very resource heavy :(

Hi sabeesh,

I did consider .Move but unfortunately I can't remove the original folder which is what that would do.

Cheers
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I borrowed what VB.NET provides with My.Computer.FileSystem.CopyDirectory, and created a stripped down class in C#, that uses a shell file operation (SHFileOperation).

Bob
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the replies guys, but as I say is there a solution where I don't have to loop back through the same method with the likes of this:
    CopyDirectory(diSrcDirectory, new DirectoryInfo(Path.Combine(diDestDir.FullName, diSrcDirectory.Name)));

As that would be very expensive on resources :(  
Would you like to see my solution?

Bob
Yes please Bob.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cheers all :)