Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activate Code Analysis by default for every project in Visual Studio 2012

Is there any way to activate code analysis feature of Visual Studio 2012 by default for every project? And, if possible to set the rules to 'Microsoft All Rules' by default.

Every time I create a new project I have to manually activate "Enable Code Analysis on Build" at the project properties and have to set the rules to all rules. Sometimes I forget to do that and have to fix more issues. I could have prevented if it was activated by default.

like image 744
ldrdl Avatar asked Dec 28 '12 09:12

ldrdl


People also ask

How do I turn off code analysis in Visual Studio?

To open this page, right-click the project node in Solution Explorer and select Properties. Select the Code Analysis tab. To disable source analysis at build time, uncheck the Run on build option. To disable live source analysis, uncheck the Run on live analysis option.

How do I disable Roslyn?

You can also disable importing external Roslyn issues in your SonarCloud project settings under General Settings / External Analyzers / C# section / Ignore issues from external Roslyn analyzers .

How do I see code analysis results in Visual Studio?

In Solution Explorer, select the project. On the Analyze menu, select Run Code Analysis on [Project Name].


1 Answers

You could edit the default project template that Visual Studio uses when you create a new project to add a common import, the problem is when it comes to prototyping do you want those settings on.

I tend to ensure that every project imports a common set of msbuild properties and item groups. This will not work by default as you would have to edit each proj file and add the import, but the advantage is that with just one line you could put so much more than just Code Analysis settings in this imported proj file, for instance, StyleCop settings, the dictionary culture that is used by Code Analysis, your own rule set file, a common AssemblyInfo.cs, snk files, etc

I tend to place the following line just before the last line in my .csproj

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

And then in the imported file something like this. This way ever 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>
like image 176
SoftwareSimian Avatar answered Sep 21 '22 03:09

SoftwareSimian