Third Shelf

Update Code Analysis Settings on all Projects

Posted in visual studio by Sydney du Plooy on August 25, 2008

On solutions with many projects (typically, more than 20), it is a rather tedious task to update and keep all the Static Code Analysis settings in Visual Studio the same across all projects. Recently, I found myself faced with the same problem and set out hunting once more.

This time Daniel Fisher (lennybacon) wrote a small macro that can be updated to enable or disable specific rules for Static Code Analysis. This macro is compatible with Visual Studio 2008.

I replicated the macro here for your ease:

Imports System
Imports System.Diagnostics
Imports System.Text
Imports System.Windows.Forms
Imports EnvDTE
Imports EnvDTE80

Public Module CodeAnalysis
  Public Sub EnableFxCop()
    Dim objProj As Object()
    Dim proj As Project

    For i As Integer = 1 To DTE.Solution.Projects.Count
      proj = DTE.Solution.Projects.Item(i)
      EnableFxCop(proj)
    Next
  End Sub

  Private Sub EnableFxCop(ByVal project As Project)
    If project.Kind = "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}" Then
      'Filter Project Folders
      For Each subProject As ProjectItem In project.ProjectItems
        EnableFxCop(subProject.SubProject)
      Next
    Else
      project.ConfigurationManager.ActiveConfiguration.Properties.Item( _
        "RunCodeAnalysis").Value = "True"

      project.ConfigurationManager.ActiveConfiguration.Properties.Item( _
        "CodeAnalysisRules").Value = String.Concat( _
        "-Microsoft.Design#CA2210;", _
        "-Microsoft.Design#CA1020;", _
        "-Microsoft.Naming#CA1705;", _
        "-Microsoft.Naming#CA1709")

      project.Save()
    End If
  End Sub
End Module

Advertisement
Tagged with: ,

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 143 other followers