Step by Step install FxCop with Visual Studio

We are going to install FxCop and integrate it with Visual Studio when a project is build. All violations of FxCop rules will be viewed in Warnings tab of Visual Studio and the build will fail.

Install FxCop : Click here

Edit the .csproj and add below tag under the last <PropertyGroup> tag and just above the child node <PostBuildEvent>

<FxCopResults>
     $(ProjectDir)obj\$(Configuration)\FxCopResults.xml
</FxCopResults>

In the <PostBuildEvent> tag add the below mentioned text

"%25ProgramFiles%25\Microsoft FxCop 10.0\FxCopCmd.exe" /file:"$(TargetPath)" /console /out:"$(ProjectDir)obj\$(ConfigurationName)\FxCopResults.xml"

In the <Target Name=”BeforeBuild”> tag add the below mentioned tag

<Delete Files="$(FxCopResults)" ContinueOnError="true" />

In the <Target Name=”AfterBuild”> tag add the below mentioned tag

<Error Text="One or more FxCop warnings occurred. Please look at the Warnings tab." Condition="Exists('$(FxCopResults)')" />

After completing all the above steps the xml in project file will look something similar to this

<Error Text="One or more FxCop warnings occurred. Please look at the Warnings tab." Condition="Exists('$(FxCopResults)')" />

Make sure your <Target Name=”BeforeBuild”> and <Target Name=”AfterBuild”> is not commented. Build your project if FxCop catches any error you will see an error as “One or more FxCop warnings occurred. Please look at the Warnings tab.”

<PropertyGroup>
 <FxCopResults>
     $(ProjectDir)obj\$(Configuration)\FxCopResults.xml
 </FxCopResults>
 <PostBuildEvent>
     "%25ProgramFiles%25\Microsoft FxCop 10.0\FxCopCmd.exe" /file:"$(TargetPath)" /console /out:"$(ProjectDir)obj\$(ConfigurationName)\FxCopResults.xml"
 </PostBuildEvent>
 </PropertyGroup>
 <PropertyGroup>
 <PreBuildEvent>
 </PreBuildEvent>
 </PropertyGroup>
 <Target Name="BeforeBuild">
   <Delete Files="$(FxCopResults)"
    ContinueOnError="true" />
 </Target>
 <Target Name="AfterBuild">
     <Error Text="One or more FxCop warnings occurred. Please look at the Warnings tab."
     Condition="Exists('$(FxCopResults)')" />
 </Target>

Happy Coding!

Advertisements