Skip to content

Instantly share code, notes, and snippets.

@jeremybeavon
Last active May 9, 2021 17:51
Show Gist options
  • Save jeremybeavon/5736e0acbf3729092887 to your computer and use it in GitHub Desktop.
Save jeremybeavon/5736e0acbf3729092887 to your computer and use it in GitHub Desktop.
Programmatically call MSBuild from C#
// Requires the following references:
// Microsoft.Build.Engine
// Microsoft.Build.Framework
using System.Collections.Generic;
using Microsoft.Build.BuildEngine;
public static class MsBuildHelper
{
public static bool RunMsBuild(string projectFile, string target, IDictionary<string, string> properties)
{
Engine engine = new Engine();
engine.RegisterLogger(new ConsoleLogger());
BuildPropertyGroup buildProperties = new BuildPropertyGroup();
foreach (KeyValuePair<string, string> property in properties)
{
buildProperties.SetProperty(property.Key, property.Value);
}
return engine.BuildProjectFile(projectFile, new [] { target }, buildProperties);
}
}
// Requires the following references:
// Microsoft.Build
// Microsoft.Build.Engine
// Microsoft.Build.Framework
using System.Collections.Generic;
using Microsoft.Build.BuildEngine;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
public static class MsBuildHelper
{
public static BuildResult RunMsBuild(string projectFile, string target, IDictionary<string, string> properties)
{
BuildParameters parameters = new BuildParameters(new ProjectCollection())
{
Loggers = new ILogger[] {new ConsoleLogger()}
};
return BuildManager.DefaultBuildManager.Build(
parameters,
new BuildRequestData(projectFile, properties, null, new[] {target}, null));
}
}
@ranuka2
Copy link

ranuka2 commented Aug 13, 2017

I tried to get a pre compiled build from this code by adding
property.Add("OutputPath", @"c:\BuildDroplocation");
property.Add("DeployOnBuild", "true");
property.Add("WebPublishMethod", "FileSystem");
property.Add("PrecompileBeforePublish", "true");
However, it placed the build in output location without precompiled, is there any additional properties need to include?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment