All posts tagged 'vs2008'

.NET Musings

Wandering thoughts of a developer, architect, speaker, and trainer


Using RoboCopy in Visual Studio Build Events

I always create a \References directory in my solutions to copy all of the assemblies to ease the tasks of:

  • Running Units Tests/Code Coverage (when not running from the IDE)
  • Have a central location for deployment
  • Make obfuscation tasks easier

To do this, open your project properties, select the Build Events tab, and click “Edit Post-Build…” (just below the Post-Build event command line text box).

When you click on the “Edit Post-Build…” button, a new dialog opens up with a button labeled “Macros >>”.  Click on this and you will see a list of helpers for your events.  These go into your command line and are variables to various files and paths in your project.  What’s nice about these is they are set based upon your build configuration, so no switching between debug/prod build voodoo is necessary.

I set mine up with RoboCopy since it is very robust and also very fast (helpful for large solutions).  If you are not familiar with RoboCopy, open a command prompt, type “RoboCopy /?” and you will see the command set.

I prefer to use are:

  • /XO = eXclude Older files
  • /NJH = No Job Header, and
  • /NP = No Progress

If I want to copy all of the .dll, .pdb, .exe, and .config files from my project, I can use a command line like this (all on one line):

robocopy $(TargetDir) $(SolutionDir)references\ *.pdb *.dll *.exe *.config /XO /NJH /NP

The first time you build this, it will succeed.  The second time you build this, it will fail.  Not because RoboCopy failed, but Visual Studio interprets any non-zero return code as a failure, while RoboCopy uses a series of flags to indicate (in nice detail) what happened.  (Google on “RoboCopy exit codes” to easily find the complete list)

Essentially, for my purposes in building a .NET project, anything above a 3 indicates there is a problem.  (Technically a 4 indicates a file system mis-match).

To pass the build, we run a simple little if statement in the command line as follows (initial RoboCopy command included to make copy/paste from this post easier):

robocopy $(TargetDir) $(SolutionDir)references\ *.pdb *.dll *.exe *.config /XO /NJH /NP
if errorlevel 4 goto BuildEventFailed
if errorlevel 0 goto end
:BuildEventFailed echo FILECOPY for $(ProjectName) FAILED
exit 1
:end echo FILECOPY for $(ProjectName) COMPLETED OK
exit 0

NOTE: Remember that first command needs to be on one line only.

Now you can quickly move the files for you project to a central repository everytime you build.

Happy Coding!

Managed Windows Shared Hosting by OrcsWeb