2

I'm trying to build a .sln in C#. I have the following code.

try
{
  Console.WriteLine("Building Solution...\n");
  string projectFileName = Directory.GetCurrentDirectory() + "\\build\\Solution.sln";
  ProjectCollection pc = new ProjectCollection();
  Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();
  GlobalProperty.Add("Configuration", "Release");
  GlobalProperty.Add("Platform", "x86");

  BuildRequestData BuidlRequest = new BuildRequestData(projectFileName, GlobalProperty, null, new string[] { "Build" }, null);

  BuildResult buildResult = BuildManager.DefaultBuildManager.Build(new BuildParameters(pc), BuidlRequest);

catch (Exception e)
{
  Console.Write("Error:" + e.Message);
  Console.ReadLine();
}
Console.WriteLine("Completed Solution build...\nPress any key to continue...");

It doesn't thow a error, it does show Completed Solution build... but I can't find the executable in \bin\Release. Am I forgetting something or how can I check it?

Alex
  • 322
  • 4
  • 18
  • Does your solution define an `x86` platform? By default, Visual C# projects usually just define `Any CPU`. – heavyd Jan 06 '15 at 16:19
  • You could be explicit: `GlobalProperty.Add("OutputPath", Directory.GetCurrentDirectory() + "\\MyOutput");` – Belogix Jan 06 '15 at 16:21
  • `Any CPU` and `GlobalProperty.Add("OutputPath", Directory.GetCurrentDirectory() + "\\build\\Solution\\bin\\Release");` didn't make any difference. – Alex Jan 06 '15 at 16:23
  • It is like, it's not building anything... – Alex Jan 06 '15 at 16:28
  • Then you might need to add a logger and check the actual output: http://stackoverflow.com/questions/9942499/building-programatically-a-project – Belogix Jan 06 '15 at 16:29

1 Answers1

6

I managed to get it working, solution is not building to due errors in the code. Anyway this is the solution.

string projectFileName = Directory.GetCurrentDirectory() + "\\build\\Solution.sln";
ProjectCollection pc = new ProjectCollection();
Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();
GlobalProperty.Add("Configuration", "Release");
GlobalProperty.Add("Platform", "Any CPU");
GlobalProperty.Add("OutputPath", Directory.GetCurrentDirectory() + "\\build\\\bin\\Release");

BuildParameters bp = new BuildParameters(pc);
bp.Loggers = new[] {
  new FileLogger
  {
    Verbosity = LoggerVerbosity.Detailed,
    ShowSummary = true,
    SkipProjectStartedText = true
  }
};

BuildManager.DefaultBuildManager.BeginBuild(bp);

BuildRequestData BuildRequest = new BuildRequestData(projectFileName, GlobalProperty, null, new string[] { "Build" }, null);

BuildSubmission BuildSubmission = BuildManager.DefaultBuildManager.PendBuildRequest(BuildRequest);
BuildSubmission.Execute();
BuildManager.DefaultBuildManager.EndBuild();
if (BuildSubmission.BuildResult.OverallResult == BuildResultCode.Failure)
  {
    throw new Exception();
  }
}
Alex
  • 322
  • 4
  • 18
  • As I thought! Glad you managed to track the issue down and if all else fails... Log! :-) – Belogix Jan 07 '15 at 09:01
  • Hi Alex, do you happen to know how I can change parameters in individual projects? I can change the AssemblyName using the globalProperty but it would overwrite every project in the sln. – tofutim Sep 21 '16 at 19:27