Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.