如何在不使用命令提示符的情况下使用MSBUILD引擎构建多个C#项目



我正在从事Windows应用程序项目,并且来自该项目需要构建不同的C#项目,这些项目是Visual Studio 2015的一个解决方案,也希望它们可以使用MSBuild工具单独构建programmatically不使用命令提示符,最终想在命令提示符中显示输出中的输出(意味着这些项目正在成功构建或在日志文件中具有任何类似此消息的错误(

我需要使用任何MSBuild API以及如何添加此项目吗?

我看到了许多这样的问题(不完全相同(,但对我不起作用。请任何人能帮助我吗?

    using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Logging;
...
 public static BuildResult Compile(string solution_name, out string buildLog)
        {
                buildLog = "";
                string projectFilePath = solution_name;
                ProjectCollection pc = new ProjectCollection();
                Dictionary<string, string> globalProperty = new Dictionary<string, string>();
                globalProperty.Add("nodeReuse", "false");
                BuildParameters bp = new BuildParameters(pc);
                bp.Loggers = new List<Microsoft.Build.Framework.ILogger>()
                {
                    new FileLogger() {Parameters = @"logfile=buildresult.txt"}
                };
                BuildRequestData buildRequest = new BuildRequestData(projectFilePath, globalProperty, "4.0",
                    new string[] {"Clean", "Build"}, null);
                BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, buildRequest);
                BuildManager.DefaultBuildManager.Dispose();
                pc = null;
                bp = null;
                buildRequest = null;
                if (buildResult.OverallResult == BuildResultCode.Success)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                }
                else
                {
                    if (Directory.Exists("C:\BuildResults") == false)
                    {
                        Directory.CreateDirectory("C:\BuildResults");
                    }
                    buildLog = File.ReadAllText("buildresult.txt");
                    Console.WriteLine(buildLog);
                    string fileName = "C:\BuildResults\" + DateTime.Now.Ticks + ".txt";
                    File.Move("buildresult.txt", fileName);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Thread.Sleep(5000);
                }
                Console.WriteLine("Build Result " + buildResult.OverallResult.ToString());
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("================================");


                return buildResult;
        }

这是我周围有一些旧代码。我用它来编程构建解决方案和C#项目。输出将是buildResult.success或buildResult.failure。变量buildlog将包含构建输出。注意 - 访问我知道的构建输出的唯一方法是使用上述方法的日志文件,然后在您的C#代码中读取它。

要注意的一件事,我从未找到过解决此问题的解决方案,是运行该代码的应用程序可能会使DLL的IT负载从Nuget Package Directories中存储在内存中。这使删除这些目录有问题。我通过将应用程序作为MS服务运行来找到工作 - 似乎当它作为本地服务运行时,它具有足够的权限来删除存储在内存中的文件。

最新更新