During the process of moving some command scripts to a more robust solution that involved C# Script, I was in the need to replace an XCOPY call with an equivalent in C#.
What I wanted to avoid is to simply start a CMD.EXE process and call the XCOPY command.
Instead I looked for a class/function in .NET/C# that can serve as a replacement. Although I found some solutions, none of them worked the way I wanted them.
Bring your own code
So I decided to develop my own small solutions that is no rocket science code but fulfills my requirements.
It has fewer features as XCOPY and is intended for copying folders (the original XCOPY works for both files and folders). In addition it is being designed to be used in a console environment (i.e. the operations are synchronously and blocking).
An example call looks like:
var options = new ZetaFolderXCopyOptions { FilesPattern = "*.*", RecurseFolders = true, CopyEmptyFolders = true, CopyHiddenAndSystemFiles = true, OverwriteExistingFiles = true, CopyOnlyIfSourceIsNewer = false, FoldersPattern = "*" } .AddExcludeSubStrings( "\\.svn\\", "\\_svn\\", "\\_Temporary\\" ); var xc = new ZetaFolderXCopy(); xc.Copy( sourceFolderPath, destinationFolderPath, options );
The full code consists of a single .CS file and can be downloaded from here for free.
Hopefully this code is of some basic usage to you, keep the feedback coming!
Update 2015-11-25
I’ve uploaded a greatly enhanced version which adds wildcard and Regex exclude support as well as retry options.
This is another step towards a native C#/.net version of robocopy.
Uwe,
Thanks for this class. Just included it in a tool for my own use (to copy a website from a build environment to a test site) and it seems to work perfectly. That saved me a couple of hours !
– Peter –
Thanks, Peter!
If you like, I have a slightly newer version that also works with paths that are longer than MAX_PATH characters.
It uses an external DLL, that I’ve written. Source code for that DLL can be found on CodeProject.com.
HI,
that’s an excellent code,can you make this code to copy with full metadata like(date created should not be changed what ever the date present in source it should also be same in destination