简体   繁体   English

如何使用C#以编程方式编译项目和解决方案?

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

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. 我找到了几种不同的方法来执行此操作,并且着手使用了Microsoft.Build.BuildEngine.Engine类,该类最近已被弃用。

Here's some sample code that illustrates its use: (note that node.Path is the path to the project file) 以下是一些示例代码,说明了其用法:(请注意,node.Path是项目文件的路径)

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. 一个提示是PropertyGroup属性,该属性似乎允许设置一些项目构建选项。.但是,尚不清楚如何获取对BuildProjectFile的结果调用以在发行版中输出。

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: 您可能会发现,使用NAnT之类的东西与NAntContrib一起使用更容易, MSBuild任务 相当全面,并且可以很容易地指定要构建的目标,例如:

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

You can also call MSBuild from the command line , for example: 您也可以从命令行调用MSBuild,例如:

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. 与msbuild相比,nant会给您带来的好处是您可以编写构建过程的脚本。 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: 您也可以直接调用Visual Studio:

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

This will also output build output to c:\\vs_errors.txt 这还将把构建输出输出到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). 您提到的设置文件应位于app.config或web.config中(取决于它是一个Web服务)。 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. 如果是这样,建议您更改编程模型以允许更改设置而无需重新编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM