One of the limitations of Visual Studio Express over other editions is that it does not support tool extensions. This includes StyleCop, so installing it will not integrate the handy right-click options in the IDE. StyleCop can still be used though, with a little bit of manual work.
By editing the .csproj files, you can have StyleCop run automatically when the project is built. This needs to be done for every project in the solution. Additionally, you can have StyleCop break the build on errors by adding a setting inside the .csproj files.
StyleCop installer can be downloaded from the official StyleCop downloads page. However, a more robust approach would be to configure and use NuGet command-line to download the StyleCop.MSBuild package. With the latter approach, neither you, nor your team has to have StyleCop installed - just use NuGet to install all dependencies and you’re set.
Note: When using the official installer, make sure you install the ‘MSBuild integration‘ component.
In order to integrate StyleCop with a project, an
<Import> needs to be added to the .csproj file (versions and paths might differ):
If you used the official installer and installed the ‘MSBuild integration‘ component, your
<Import>will look something like this:
<Import Project="$(ProgramFiles)\MSBuild\StyleCop\v4.7\StyleCop.targets" />
If you used NuGet to obtain the StyleCop.MSBuild package and installed it under the solution’s vendor sub-directory, your
<Import>will look like this:
<Import Project="$(SolutionDir)\vendor\StyleCop.MSBuild.22.214.171.124\tools\StyleCop.Targets" />
Open the .csproj in a text editor and add the new
<Import> below the existing one to include the StyleCop.targets file:
... <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- Your StyleCop <Import> will go here --> ...
Note: Make sure you replace the version numbers to match your setup.
Save the .csproj file and reload the project. If you made any mistakes, they will be reported on startup. At this point, StyleCop will show warnings (if there are any) when you build your project.
In the StyleCop installation directory (or one obtained from NuGet) you will find two important files:
- Settings.StyleCop - Default configuration of StyleCop rules
- StyleCopSettingsEditor.exe - Editor for the .StyleCop files
Note: StyleCopSettingsEditor.exe requires that a file name is passed as the command-line argument. If you get the error: ‘The command line arguments are incorrect. Include the path to a StyleCop settings file.‘, just drag and drop Settings.StyleCop on StyleCopSettingsEditor.exe.
You can copy Settings.StyleCop file into your solution and project directories - the closest one to the project will be used. In the following example, FooProject is affected by the global solution settings while BarProject uses its own settings file:
- Settings.StyleCop (solution wide settings)
- Settings.StyleCop (BarProject specific settings)
By default, StyleCop shows all errors as warnings. To change this, add
<StyleCopTreatErrorsAsWarnings> setting to a
<PropertyGroup> inside the .csproj file:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|(platform)' "> <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings> ...
A recommended approach is to add this setting to the Release configuration
<PropertyGroup>. This way, the Release build would fail on errors, but you could still build the Debug configuration with warnings.