Integrate FxCop 1.36 & VS 2008
Posted by Vadim on May 9, 2009
Update: Visual Studio Team System (VSTS) has Code Analysis feature. It’s not FxCop but it does serve the same purpose and is built using the same engine as FxCop. Unfortunately I’m working with VS Professional that doesn’t include Code Analysis.
I was looking for way to integrate FxCop 1.36 with Visual Studio 2008. I even asked the same question on StackOverflow. The only answer I got was a link to MSDN article. I follow direction in the article but wasn’t successful. I assume that if you have only one project and don’t have a solution it will work for you but it’s not my case. I have many project in my solution. However, the MSDN article got me going. Let me present you my solution of integration.
Create FxCop project using FxCop GUI.
Important:
- Save the project file in the same directory where your solution is.
- Project name must be the file name of your solution (including .sln extension). For example: If your solution file is MySolution.sln, the FxCop project name must be MySolution.sln.FxCop.
Add FxCop to VS External Tools.
In Visual Studio select Tools->External Tools… You should see the External Tools dialog box.
You must fill following fields:
| Title: | FxCop |
| Command: | C:\Program Files\Microsoft FxCop 1.36\FxCopCmd.exe |
| Arguments: | /c /p:"$(SolutionDir)\$(SolutionFileName).fxcop" /cXsl:"C:\Program Files\Microsoft FxCop 1.36\Xml\VSConsoleOutput.xsl" |
| Initial Directory: | C:\Program Files\Microsoft FxCop 1.36 |
| Use output window | Must be checked |
If you install FxCop in different location than C:\Program Files\Microsoft FxCop 1.36, you need to use your own path.
In case you want to grok the switches we use, you can read more about FxCopCmd options.
Run FxCop from Visual Studio.
We are done. Now we just need to run FxCop.
Tools –> FxCop should analyze your code and because we checked “Use Output window” in External Tools dialog result should appear in VS output window.



Sean Hederman said
Ummm, it’s ALREADY integrated with VS 2008, so there’s actually no call for what you’re doing, which is perhaps why you found so little information.
Simply open up the project Properties page, click on the Code Analysis tab and check the “Enable Code Analysis on Build” checkbox, OR right click on a project in the Solution Explorer and select “Run Code Analysis”.
Vadim said
Sean,
You’re talking about Code Analysis that is part of VS Team System. However, I’m using Visual Studio Professional that missing this feature.
Sean Hederman said
Really? I honestly thought Professional had it. It’s quite shocking that it doesn’t.
Jeff Klawiter said
I haven’t heard but I’m really hoping the move it down to VS 2010 pro. This is one of those must have features for a “professional” developer. I’m still shocked myself that fxCop has such horrible VS Pro integration. StyleCop integrates fully. It’s easy to use and gives warnings and errors that can stop a build. The price point of going from Pro to TS is quite painful and I’ve yet to be able to convince my bosses to pony up.
Peter Smith said
Another factor is that this method does it at the solution level, not the project level. My solution has 39 projects. You do the math. :) I’m both happy to see this version, AND to find that “Run Code Analysis” exists at the project level; I rarely do ANYTHING at the project level (from the perspective of right-clicking and property pages).
Reflective Perspective - Chris Alcock » The Morning Brew #344 said
[...] Integrate FxCop 1.36 & VS 2008 – Vadim Kreynin shows how you can integrate FxCop 1.36 into your Visual Studio 2008 Builds with some simple instructions. [...]
Technology Related Links for May 11th - Jason N. Gaylord's Blog said
[...] Integrate FxCop 1.36 & VS 2008 – Vadim Kreynin (Suggested by Elijah Manor) [...]
Maithree said
I dont get the Use Output Window option enabled.
Abessinierkatze said
I had the same problem,
but the solution ist very easy:
use the FxCopCmd.exe instead of the FxCop.exe
And you will see, the checkbox will be enabled.
Thanks for this tip
Michael Sync said
It doesn’t work .. i can’t see FxCop in VS External Tools..
Vadim said
Have you tried to add FxCop to External Tools?
RT said
Thanks for the very helpful article.
I ran into a couple of issues and here is how I resolved them.
1. When creating the FxCop project, I didn’t specify the DLLs to be analyzed. I expected those to be picked up automatically. No dice. So, I explicitly specified the assemblies using the menu option: Project, Add Targets
2. I was analyzing a MVC 1.0 solution and that referenced the System.Web.MVC.dll. By default, FxCop didn’t resolve that reference. So, I chose the Project, Options, Spellings and Analyze tab and checked “Search GAC for missing references”
3. I didn’t specify the FxCop extension when saving the FxCop project file and that overwrote the solution file itself. So, I specified the FxCop extension and it created the FxCop project file.
Kev said
I did exactly what you mentioned in the article. In the arguments column I eneterd
/c /p:”$(SolutionDir)\$(SolutionFileName).fxcop” /cXsl:”C:\Program Files\Microsoft FxCop 1.36\Xml\VSConsoleOutput.xsl”
Then when I click fxcop in the external tools after build I get.
Microsoft (R) FxCop Command-Line Tool, Version 1.36 (9.0.30729.1)
Copyright (C) 2007 Microsoft Corporation. All rights reserved.
Loading D:\Myfxapp\\Myfxapp.sln.fxcop…
Loaded NamingRules.dll…
Loaded PerformanceRules.dll…
Loaded UsageRules.dll…
* Analysis was not performed; at least one valid rules assembly and one valid
target file must be specified.
* 1 total analysis engine exceptions.
Project : error : CA0052 : No targets were selected.
Done.
khader said
Any reply you got if you already fixed this please can you….help me to fix the same
Lewis Moten said
I put the following command in my post-build event command line on each project for VS.Net Pro. It shows me all the warnings, but as for navigating to the file itself, it says “Location not stored in Pdb”
“%ProgramFiles%\Microsoft Fxcop 10.0\FxCopCmd.exe” /file:”$(TargetPath)” /console
ajay said
Loading D:\Myfxapp\\Myfxapp.sln.fxcop…
Loaded NamingRules.dll…
Loaded PerformanceRules.dll…
Loaded UsageRules.dll…
* Analysis was not performed; at least one valid rules assembly and one valid
target file must be specified.
* 1 total analysis engine exceptions.
khader said
I have setup same thing and started running Fx Cop, getting below error
Microsoft (R) FxCop Command-Line Tool, Version 1.36 (9.0.30729.1)
Copyright (C) 2007 Microsoft Corporation. All rights reserved.
\Proactive-Complete.sln.fxcop…
Loaded DesignRules.dll…
Loaded GlobalizationRules.dll…
Loaded InteroperabilityRules.dll…
Loaded MobilityRules.dll…
Loaded NamingRules.dll…
Loaded PerformanceRules.dll…
Loaded PortabilityRules.dll…
Loaded SecurityRules.dll…
Loaded UsageRules.dll…
* Analysis was not performed; at least one valid rules assembly and one valid
target file must be specified.
* 1 total analysis engine exceptions.
Project : error : CA0052 : No targets were selected.
Done.
Bijay said
Can i analyse a solution file? Because I my solution contains nearly 10 projects and I do not want to analyse individual projects.
Jaqueline said
Hi would you mind sharing which blog platform you’re using? I’m looking
to start my own blog soon but I’m having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking
for something unique. P.S My apologies for
being off-topic but I had to ask!
Kiquenet said
Can I use fxcopcmd.exe and SupressMessage attribute in my source code in assembly ?