Rory Primrose

Learn from my mistakes, you don't have time to make them yourself

View project on GitHub

Using StyleCop

Posted on November 18, 2008

SC is a great tool for keeping code style consistent. I don’t agree with all of the default rules though. Rules that you don’t want can be disabled using a Settings.StyleCop file. By default, each project will use its own settings file. Having to maintain a settings file for each project is very cumbersome. Thankfully, there is a better way.

The Source Analysis blog has a post about how settings are inherited from parent directories. This makes sharing settings between projects very easy. Here are some ideas for how to set this up and make the settings file easy to work with.

These instructions assume that all the projects in the solution are in child directories under the location that the solution file resides in.

How to manage your StyleCop settings

If a Settings.StyleCop file doesn’t already exist, right-click on a project and select StyleCop Settings. Remove any of the rules that you don’t want applied and click OK.

Open Windows Explorer for that project and move the Settings.StyleCop file into the parent (solution) directory.

Right-click on the solution in Solution Explorer and select Add -> Existing Item.

Select Settings.StyleCop from the solution directory.

Add Existing Item

This will add a link to this file into the solution, but not specific to a project. For example:

Add as Solution Item

The next step is to ensure that the default application for this file in Visual Studio is the SC settings editor.

Right-click the Settings.StyleCop file and select Open With.

Settings Editor

Ensure that StyleCopSettingsEditor is set as the default. If it isn’t, select it and click Set as Default_and click _OK.

From now on, you can simply open the Settings.StyleCop file like any other file and it will open up in the settings.

Note: Using this configuration, don’t edit the settings for StyleCop against the project directly as these settings will not be available to the other projects in the solution.

This is my current settings file: Settings.StyleCop (2.94 kb)