Activate default code analysis for each project in Visual Studio 2012

Is there a way to activate the default Visual Studio 2012 code analysis feature for each project? And, if possible, by default, set the "All Microsoft Rules" rules.

Each time I create a new project, I need to manually activate "Enable code analysis during assembly" in the project properties and set the rules for all the rules. Sometimes I forget to do this and have to fix more problems. I could prevent if it was activated by default.

+4
source share
1 answer

You can edit the default project template that Visual Studio uses when creating a new project to add general imports, the problem is when it comes to prototyping, you want these options enabled.

I try to make every project import a common set of msbuild properties and item groups. This will not work by default, since you will need to edit each proj file and add import, but the advantage is that with just one line you could add much more than just code analysis parameters in this imported proj file, for example StyleCop settings, the dictionary culture used by Code Analysis, its own rule set file, shared AssemblyInfo.cs, snk files, etc.

I usually put the next line immediately before the last line in my .csproj

<Import Project="..\Build\MyCompanySettings.proj" /> 

And then in the imported file something like this. Thus, ever a project has the same settings.

 <?xml version="1.0" encoding="utf-8" ?> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <RunStyleCop>true</RunStyleCop> <StyleCopOverrideSettingsFile>..\Build\Settings.StyleCop</StyleCopOverrideSettingsFile> <RunCodeAnalysis>true</RunCodeAnalysis> <TreatWarningsAsErrors>true</TreatWarningsAsErrors> <SignAssembly>true</SignAssembly> <AssemblyOriginatorKeyFile>..\Build\MyCompany.snk</AssemblyOriginatorKeyFile> <CodeAnalysisRuleSet>..\Build\MyCompany.ruleset</CodeAnalysisRuleSet> <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings> <SkipPostSharp>True</SkipPostSharp> <CodeAnalysisCulture>en-GB</CodeAnalysisCulture> </PropertyGroup> <ItemGroup> <None Include="..\Build\MyCompany.snk"> <Link>MyCompany.snk</Link> </None> <CodeAnalysisDictionary Include="$(BuildTargetsDirectory)\MyCompanyCustomDictionary.xml"> <Link>Properties\MyCompanyCustomDictionary.xml</Link> </CodeAnalysisDictionary> <Compile Include="..\Build\VersionInfo.cs" > <Link>Properties\VersionInfo.cs</Link> </Compile> <None Include="..\Build\MyCompany.ruleset" > <Link>MyCompany.ruleset</Link> </None> </ItemGroup> <Import Project="$(MSBuildProgramFiles32)\MSBuild\StyleCop\v4.7\StyleCop.targets" Condition="'$(RunStyleCop)' == 'true'"/> </Project> 
+6
source

All Articles