简体   繁体   中英

How to build multiple c# projects using MSBuild engine without using command prompt?

I am working on windows application project and from that project want to build different multiple c# projects which are in one solution of visual studio 2015 and also want them to be build programmatically individually using MSBuild tool without using command prompt and finally want to show the output in log file not in command prompt (means those project is building successfully or having any errors like this message in log file)

Do I need to use any MSBuild API and how to add in this project?

I have seen many questions like this (not exactly same) but it didn't work for me. please can anybody help me with this?

    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;

        }

This is some old code I had lying around. I use this to programatically build solutions and C# Projects. The output will be a BuildResult.Success or BuildResult.Failure. The variable buildLog will contain the build output. Note - the only way to access the build output that I am aware of is to use the above methodology of having a log file generated and then reading it in your C# code.

One thing to be aware of and I never did find a fix for this, is that the application that runs this code, may keep dll's it loads into memory from nuget package directories in memory. This makes deleting those directories problematic. I found a work around by having my application run as a MS Service - it seems when it runs as a local service, it has enough permissions to delete files held in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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