Edit csproj Project file programatically

In my current engagement, we have more than 80 projects in a solution (don’t ask me why :)). Recently, as per quality guidelines, we needed to make few changes to each project.
For example:  Treat warnings as errors, enable code analysis for each project, sign assembly etc.

I realized doing it manually can take me entire day so I spent few mins to create a small script in C# to save my time. Here is the code snippet:

using System.Collections.Generic;
using System.Linq;
using Microsoft.Build.Evaluation;
class Program
static void Main(string[] args)
var projectList = new List<string>()
// Your Project file paths
foreach (var project in projectList)
var projectCollection = new ProjectCollection();
var proj = projectCollection.LoadProject(project);
// Select Debug configuration
var debugPropertyGroup =
e => e.Condition == " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ");
debugPropertyGroup.SetProperty("TreatWarningsAsErrors", "true");
debugPropertyGroup.SetProperty("RunCodeAnalysis", "true");
// Select Release configuration
var releasePropertyGroup =
e => e.Condition == " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ");
releasePropertyGroup.SetProperty("TreatWarningsAsErrors", "true");
releasePropertyGroup.SetProperty("RunCodeAnalysis", "true");
//Sign assembly with a with strong name key
proj.SetProperty("SignAssembly", "true");
proj.SetProperty("AssemblyOriginatorKeyFile", "test.pfx");
view raw EditCsProj.cs hosted with ❤ by GitHub

Hope it helps save some time for some of you 🙂

Update: I have created a command line utility for the solution and added it to GitHub. This utility accepts different set arguments to based on operations required to be performed. Please refer to ReadMe.md in GitHub for more details.





2 responses to “Edit csproj Project file programatically”

  1. Anonymous Avatar

    Hi, I tried your code but it’s replacing $(configuration) to ‘debug’. Is there a way to avoid it.


    1. Ankit Vijay Avatar

      You can, just change line
      var debugPropertyGroup =
      e => e.Condition == ” ‘$(Configuration)|$(Platform)’ == ‘Debug|AnyCPU’ “);
      Instead of Debug, provide your own conifguration


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

A WordPress.com Website.

%d bloggers like this: