XCOPY/ROBOCOPY like class in C#

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.