Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
Created September 28, 2017 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankitvijay/0d8003f244fb4e9bcd6cd91717f34eab to your computer and use it in GitHub Desktop.
Save ankitvijay/0d8003f244fb4e9bcd6cd91717f34eab to your computer and use it in GitHub Desktop.
Edit Csproj Progamatically
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 =
proj.Xml.PropertyGroups.First(
e => e.Condition == " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ");
debugPropertyGroup.SetProperty("TreatWarningsAsErrors", "true");
debugPropertyGroup.SetProperty("RunCodeAnalysis", "true");
// Select Release configuration
var releasePropertyGroup =
proj.Xml.PropertyGroups.First(
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");
//Save
proj.Save();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment