How to build a project using C# 4.0?

If you want to build a project using C# in .NET 4.0 then you should use two important namespaces:

using Microsoft.Build.Evaluation;
using Microsoft.Build.Logging;

After you get an instance to the global project collection, you need to load you project.

var projectCollection = ProjectCollection.GlobalProjectCollection;
var project = projectCollection.LoadProject(@"d:\Path\To\Project\My.Web.csproj");

Setting the logger is an important step too.

var fileLogger = new FileLogger();
fileLogger.Parameters = @"logfile=" + logfilePath;

Start the build process and grab the result. After that make sure you unregister the logger.

bool result = project.Build();
return result;