views:

696

answers:

4

I would like to compile a solution by passing the solution file path (.sln file) and the build mode (debug, release). I do not want to call a command line process like devenv.exe or msbuild.exe, instead I would like to use an API and know if there were compile errors.

Is it possible ? Please provide samples if you think you know how to do that.

A: 

As itowlson pointed out you could also use the Build Engine APIs

... from MSDN ...

// Build a project file
bool success = engine.BuildProjectFile(@"c:\temp\validate.proj");

You can use MSBuild from a script such as a batch file. And just check the ErrorLevel for something other than 0... or you can have fun with the APIs.

C:\Windows\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe

.. from msbuild.exe /? ...

Microsoft (R) Build Engine Version 2.0.50727.4927 [Microsoft .NET Framework, Version 2.0.50727.4927] Copyright (C) Microsoft Corporation 2005. All rights reserved.

Syntax: MSBuild.exe [options] [project file]

...

Examples:

    MSBuild MyApp.sln /t:Rebuild /p:Configuration=Release
    MSBuild MyApp.csproj /t:Clean /p:Configuration=Debug
Matthew Whited
Since he specifically doesn't want to use the MSBuild command line, probably worth pointing him at the MSBuild API, in particular http://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.engine.buildprojectfile.aspx.
itowlson
Good point... I would of probably just wrapped the exe with a Process.Start() but I could see the advantages in the API.
Matthew Whited
+1  A: 

here is an article discussing using the C# compiler programmatically. Here is an article discussing the sln file format. The sln file is an xml file, so its pretty easy to read it.

Andrew Keith
+3  A: 

You're going to have to launch a process at some point to do the compilation you might as well spawn a process to kick off the compilation. It is going to be much easier than struggling with an API.

Edit Andrew gives some good advice about how you might go about it but I still think it is a mistake.

stimms
It is amazing how easy an exit code can be to script with. (I think too many people don't remember how simple and easy batch files are to work with... then again it's been a few years since DOS was popular so maybe they just don't know.)
Matthew Whited
Fair enough that sounds indeed much easier that using the API.I think it is a shame there is no easy .net API though with a proper object returned where we could test for errors. etc.Thank you
clems
Guess you missed the Build Engine APIs that itowlson pointed out http://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.engine.buildprojectfile.aspx
Matthew Whited
@Matthew I did miss that, an interesting idea.
stimms
This api does not seem that easy to use with a .sln file.Ideally I'd like a method that takes a .sln file
clems
A: 

I'd also suggest using the MSBuild API. It seems to work quite well. However, it has significantly changed as of .NET4 and VS2010.

CJBrew