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;
projectCollection.RegisterLogger(fileLogger);

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

bool result = project.Build();
projectCollection.UnregisterAllLoggers();
return result;
Advertisements

6 thoughts on “How to build a project using C# 4.0?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s