开发者

How do I programmatically compile projects and solutions in release with c#?

开发者 https://www.devze.com 2023-01-10 17:06 出处:网络
My dilemma is that I need to compile a large amount of projects and solutions as per the configuration settings specified in the files. I\'ve found a few different ways to do this, and I\'ve landed on

My dilemma is that I need to compile a large amount of projects and solutions as per the configuration settings specified in the files. I've found a few different ways to do this, and I've landed on using the Microsoft.Build.BuildEngine.Engine class, which has recently become deprecated.

Here's some sample code that illustrates its use: (note that node.Path is the path to the project file)

var builder = new Engine { BinPath = @"C:\Windows\Microsoft.NET\Framework\v3.5" };
var logger = new FileLogger { Parameters = @"logfile=" + Path.Combine(logdir, Path.GetFileName(node.Path)) + ".txt" };

compiler.RegisterLogger(logger);
bool success = compiler.BuildProjectFile(node.Path);
compiler.UnregisterAllLoggers();

return success;
开发者_如何学运维

My problem - it compiles in debug :(

Looking through the members of the class hasn't helped much as there aren't that many properties exposed. The one hint was the PropertyGroup property which seems to allow the setting of some project build options.. however it isn't clear how to get the resulting call to BuildProjectFile to output in release.

I'd really appreciate any help with this!


Do you have to do this through code? You may find it easier to use something like NAnT, along with NAntContrib, the MSBuild task is fairly comprehensive and makes it quite easy to specify which target to build, for example:

  <msbuild project="${ProjectBasePath}\${ProjectName}.sln">
    <property name="Configuration" value="debug"/>
  </msbuild>

You can also call MSBuild from the command line, for example:

msbuild.exe project.proj /t:rebuild /p:Configuration=Debug

The advantage nant would give you, over msbuild is that you can script your build process. I, personally, find it quite easy to get on with and you can get a lot of power and flexibility out of it.

You can also call Visual Studio directly:

devenv.exe /Rebuild release "ProjectName.sln" /Out "c:\vs_errors.txt"

This will also output build output to c:\vs_errors.txt


I don't understand why you need to constantly compile. Settings files as you mention should be in app.config or web.config (depending it's a webservice). Settings files should not change the compilation of the app code. If it is I suggest you change your Programming model to allow for changing settings without recompiling.

0

精彩评论

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