FAQ: How do I share Managed Code Analysis rule settings over multiple projects? [David Kean]

If your team has a minbar of Managed Code Analysis rules that must be explicitly fixed or suppressed, it is possible to share the Managed Code Analysis rule settings over multiple MSBuild projects (.csproj, .vbproj).

To share the minbar between multiple projects, do the following:

  1. Using Visual Studio, create a new empty project

  2. In Solution Explorer, right-click the project and choose Properties

  3. In the Project Properties window, choose the Code Analysis tab

  4. In the Code Analysis tab, choose the rules you want for your minbar

  5. In Solution Explorer, right-click the project and choose Unload Project, answering Yes to any prompt to save changes

  6. In Solution Explorer, right-click the project and choose Edit

  7. Search for the <CodeAnalysisRules> element and copy the text within it

  8. Create an empty text file called [team].CodeAnalysis.Rules.targets, replacing [team] with the name of your team

  9. In the text file enter the following, replacing [rulesettings] with the text copied above in Step 6:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <CodeAnalysisRules>$(CodeAnalysisRules); [rulesettings]< /CodeAnalysisRules> </PropertyGroup> </Project>

    If you do not want developers to be able to turn on extra rules over and above the minbar, do not add the ' $(CodeAnalysisRules); ' text.

  10. Using Visual Studio, open your projects

  11. In Solution Explorer, right-click a project and choose Unload Project

  12. In Solution Explorer, right-click the unloaded project and choose Edit

  13. Add the following <Import> element to the project, replacing [path] with the path of the targets file created above. This needs to be the last line (just above the closing </Project> element) to ensure that settings within the project do not override the team settings:

     <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 


       <ImportProject=" [path] " /> </Project>

  14. Repeat from Step 11 for each project you want to share the rule settings with

Any projects that imported the common rule settings via Step 9, should now reflect these settings in the Code Analysis properties pane. You can also share any MSBuild properties using similar steps.

Please Note:  In order to avoid Visual Studio prompting about unsafe imported MSBuild projects, you need to explicitly trust the [team].CodeAnalysis.Rules.targets file. To do this, use the following:

  1. Run regedit
  2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\MSBuild\SafeImports
  3. Add a new string value called [team].CodeAnalysis.Rules.targets and set its value to the full path of the targets file.