Skip to content

Instantly share code, notes, and snippets.

@iskeld
Created December 1, 2013 21:16
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 iskeld/7740896 to your computer and use it in GitHub Desktop.
Save iskeld/7740896 to your computer and use it in GitHub Desktop.
A simple MSBuild Task to modify the project file properties.
using System.IO;
using System.Linq;
using Microsoft.Build.Construction;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace EldSharp.Scripts.MsBuild
{
public sealed class ProjectPropertyWriterTask : Task
{
[Required]
public string ProjectFile { get; set; }
[Required]
public string Property { get; set; }
[Required]
public string Value { get; set; }
public override bool Execute()
{
if (!File.Exists(ProjectFile))
{
Log.LogError("Project file {0} does not exist", ProjectFile);
return false;
}
ProjectRootElement root = ProjectRootElement.Open(ProjectFile);
if (root == null)
{
Log.LogError("Invalid project file: {0}", ProjectFile);
return false;
}
var foundProperties = root.Properties.Where(p => p.Name == Property);
foreach (ProjectPropertyElement property in foundProperties)
{
property.Value = Value;
}
root.Save();
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment