How to run msbuild from your application ?

The following function creates an Engine object and uses the BuildProject method to build a project file.

scriptParams )
{
    // Instantiate a new Engine object
    Engine engine = new Engine();
 
    // Point to the path that contains the .NET Framework 2.0 CLR and tools
    engine.BinPath =
        System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
 
 
    // Instantiate a new FileLogger to generate build log
    FileLogger logger = new FileLogger();
 
    // Set the logfile parameter to indicate the log destination
    logger.Parameters = "logfile="+logPath;
 
    // Register the logger with the engine
    engine.RegisterLogger(logger);
 
    Project project = new Project(engine);
 
    project.Load(msbuildScriptPath);
 
    
    // pass params to msbuild file
    foreach (BuildPropertyGroup pg in project.PropertyGroups)
    {
        foreach ( string key in scriptParams.Keys )
        {
            pg.AddNewProperty( key, scriptParams[key]);
        }
        break;
    }
    
    // Build a project file
    bool success = engine.BuildProject(project);
 
    //Unregister all loggers to close the log file
    engine.UnregisterAllLoggers();
 
 
    return success;
}


 
 
 

Leave a Reply