FAQ: How do I run FxCop during a post-build event?

A little known feature of FxCop (in particular FxCopCmd.exe) is its ability to be integrated into the build process within Visual Studio. Although not a replacement for the Code Analysis functionality available within both Visual Studio Team Edition for Developers and Visual Studio Team Suite, it allows you to display FxCop violations within the Error List alongside normal build errors and warnings.

To set this up is easy:

  1. In Solution Explorer, right-click on your project and choose Properties

  2. For C# projects, in the Project Properties window, select the Build Events tab
    For Visual Basic projects, in the Project Properties window, select the Compile tab, and click Build Events

  3. In the Post-build event command-line text box, enter the following (assuming you installed FxCop to the default location):

    "%ProgramFiles%\Microsoft FxCop 1.35\FxCopCmd.exe" /file:"$(TargetPath)" /console

  4. In Solution Explorer, right-click on your project and choose Build

Any FxCop violations will now appear as build warnings:

FxCop In ErrorList

One thing that you might notice is that although source information is populated in the Error List for warnings raised against properties, events and methods, double-clicking the warning does not cause Visual Studio to jump its location. It turns out this is actually a bug in the MSBuild engine failing to assume the default column of 1 for warnings without column information. This is likely to be fixed in Visual Studio Orcas. 

The workaround for this is to simply replace %ProgramFiles%\Microsoft FxCop 1.35\Xml\VSConsoleOutput.xsl (again, assuming you installed to the default location), with the version attached to this post.

Thanks to David Gardiner for making us aware of both the bug and workaround.


Skip to main content