BlackWaspTM
Visual Studio
VS 2005+

Treating Warnings as Errors in Visual Studio (2)

The compiler used by Visual Studio can produce errors when code cannot be built and warnings when it completes compilation successfully but there are areas of concern. To avoid warnings being missed or ignored, there is an option to treat them as errors.

Treating Specific Warnings as Errors

In addition to the "None" and "All" options, you can elect to treat only specific warnings as errors. This is useful when you wish to permit most warnings but where selected warnings are not acceptable. To use this option, select the "Specific warnings:" radio button and provide a comma-separated list of warning numbers in the textbox. You may have noticed in the Output window that the warning generated when compiling included the warning number. The message will be similar to the following:

warning CS0612: 'ConsoleApplication1.Program.ObsoleteMethod()' is obsolete

To specify that only this type of warning should be treated as an error, add "612" to the textbox in the project properties.

Allowing Specific Warnings

Some developers prefer to treat all warnings as errors except for one or two specific warning types. Using the IDE, this would require that almost the entire list of warning numbers would need to be included in the "Specific warnings:" textbox for each project. This would be awkward to configure.

It is possible to exclude specific warnings by editing the project file directly using a text editor such as notepad. C# project files can be found in the project's folder and have the extension, "csproj". Before editing the file, switch on the option to treat all warnings as errors and then save your project. Open the project file in Notepad or your preferred editor and you will find a series of "PropertyGroup" XML tags. To ignore specific errors, add a new PropertyGroup tag and include within it another tag named "WarningsNotAsErrors". You can then list the warning numbers that should remain as warnings.

<PropertyGroup><WarningsNotAsErrors>612</WarningsNotAsErrors></PropertyGroup>

NB: It is advisable to backup the file before editing it.

25 June 2009