开发者

How can I programmatically build a Visual Studio solution?

开发者 https://www.devze.com 2022-12-11 06:18 出处:网络
I would like to compile a solution by passing the solution file path (.sln file) and the build mode (debug, release).

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.


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

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


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


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 it's pretty easy to read it.


I'd also suggest using the MSBuild API. It seems to work quite well. However, it has significantly changed as of .NET 4 and Visual Studio 2010.

0

精彩评论

暂无评论...
验证码 换一张
取 消