1

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?

1

1 Answer 1

1
    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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @Baaleos Perfect solution :) (Note- Ignore above ques)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.