angus_young_acdc
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :(
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 :(
ASKER
Hi sabeesh,
I did consider .Move but unfortunately I can't remove the original folder which is what that would do.
Cheers
I did consider .Move but unfortunately I can't remove the original folder which is what that would do.
Cheers
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I borrowed what VB.NET provides with My.Computer.FileSystem.Cop yDirectory , and created a stripped down class in C#, that uses a shell file operation (SHFileOperation).
Bob
Bob
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(diSrcDirecto ry, new DirectoryInfo(Path.Combine (diDestDir .FullName, diSrcDirectory.Name)));
As that would be very expensive on resources :(
CopyDirectory(diSrcDirecto
As that would be very expensive on resources :(
Would you like to see my solution?
Bob
Bob
ASKER
Yes please Bob.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cheers all :)
DirectoryInfo dirInfo = new DirectoryInfo("sourcepath"
dirInfo.MoveTo("destDirNam