There are multiple ways to cause StyleCop to ignore a file. You might want to do this for instance, if you turn on StyleCop for a legacy project. Rather than spending hours getting every file in the legacy project to pass StyleCop, you can have immediate benefits by turning it on for the project, and then selectively turning it off for the legacy files in the project that violate StyleCop. This means that any new files added to the project can have StyleCop enforced by default.

Option 1 - The "ExcludeFromStyleCop" element in a .csproj file

You can quickly force StyleCop to ignore files in a project by manually modifying the project file, and inserting the following element under each file being compiled that you wish to have ignored:

<Compile Include="AViolatingFile.cs">

This option is particularly nice if you want to turn off StyleCop for many files in a project - think legacy projects. You only need to edit one file instead of many. This method is described in StyleCop’s documentation for working with legacy projects.

Option 2 - the auto-generated file header comment

StyleCop will ignore any file that is auto-generated by a tool. You can take advantage of this fact by tricking StyleCop into thinking your code is auto-generated. This is obviously a kludge, but if you really wanted to do this, you simply add the following one line comment to the top of your code file:

// <auto-generated/>

Option 3 - the generated code #region directive

StyleCop will ignore #regions of code that are auto-generated by a tool. This is another kludge, but to take advantage of this fact, you just need to add a region ending with the text “generated code”. The example given in the StyleCop documentation is that of a typical InitializeComponent method. For instance:

#region Component Designer generated code
/// <summary> 
/// Required method for Designer support - do not modify 
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()

You can extend one of these “generated code” #regions to encapsulate your entire class file if you wish.