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 =
    // 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
    Project project = new Project(engine);
    // pass params to msbuild file
    foreach (BuildPropertyGroup pg in project.PropertyGroups)
        foreach ( string key in scriptParams.Keys )
            pg.AddNewProperty( key, scriptParams[key]);
    // Build a project file
    bool success = engine.BuildProject(project);
    //Unregister all loggers to close the log file
    return success;


Leave a Reply