aarn aarn - 1 month ago 26
C# Question

Programmatically build project - how to add reference path

ProjectCollection pc = new ProjectCollection();

// THERE ARE A LOT OF PROPERTIES HERE, THESE MAP TO THE MSBUILD CLI PROPERTIES
Dictionary<string, string> globalProperty = new Dictionary<string, string>();
globalProperty.Add("Configuration", "Debug");
globalProperty.Add("Platform", "AnyCPU");
globalProperty.Add("OutputPath", @"c:\Output");

FileLogger logger = new FileLogger();
logger.Parameters = string.Format(@"logfile=C:\build.log");
BuildParameters bp = new BuildParameters(pc);
bp.Loggers = new List<ILogger>() { logger };
BuildRequestData buildRequest = new BuildRequestData(buildFileFullName, globalProperty, "4.0", new string[] { "Build" }, null, BuildRequestDataFlags.ReplaceExistingProjectInstance);
// THIS IS WHERE THE MAGIC HAPPENS - IN PROCESS MSBUILD
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, buildRequest);
// A SIMPLE WAY TO CHECK THE RESULT
if(buildResult.OverallResult == BuildResultCode.Success)
{
Console.WriteLine("ok");
}
else
{
Console.WriteLine("wrong");
}


Hi, I need add reference path before execution building (like in the visual). How should I do this.

Answer

You can use Assembly.LoadFrom Method Like this (Loads an assembly given its file name or path.):

Assembly assembly = Assembly.LoadFrom("Path of the DLL");