Turn on "Warnings as Errors" for all projects in a solution
It's a habit of mine to always try to address any compiler warnings, as more often than not a warning is an indication that something is wrong with your code (even though it is still may be compilable). One way to ensure this is to make warnings cause a build to fail (just like errors do).
For a single project, you can turn this on within the Build tab of the Project Properties page:
If you have a large solution with lots of existing projects, this can be a bit tedious so I wrote a PowerShell script to automate the process. It finds all C# projects, and for each .csproj file updates every configuration block found and adds a new TreatWarningsAsErrors
element. (If you don't use TFS then just comment out the line that calls TF.EXE)
Get-ChildItem -Recurse -Filter "*.*csproj" | % {
Write-Host $_.Name
$filename = $_.Fullname
$proj = [xml]( Get-Content $_.Fullname )
$xmlNameSpace = new-object System.Xml.XmlNamespaceManager($proj.NameTable)
$xmlNameSpace.AddNamespace("p", "http://schemas.microsoft.com/developer/msbuild/2003")
$nodes = $proj.SelectNodes("/p:Project/p:PropertyGroup[@Condition and not (p:TreatWarningsAsErrors)]", $xmlNameSpace)
$touched = $false
$nodes | ForEach-Object -Process {
$e = $proj.CreateElement("TreatWarningsAsErrors", "http://schemas.microsoft.com/developer/msbuild/2003")
$e.set_InnerText("true")
$_.AppendChild($e) | Out-Null
$touched = $true
}
if ($touched) {
Write-Host "Checkout $filename"
& "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\TF.exe" checkout $filename | Out-Null
$proj.Save("$($filename)") | Out-Null
}
}
Categories: .NET, PowerShell