在C#可执行文件中构建.sln

时间:2015-01-06 16:13:17

标签: c# msbuild microsoft.build

我试图在C#中构建一个.sln。我有以下代码。

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...");

它没有发生错误,它确实显示Completed Solution build...,但我无法在\bin\Release中找到可执行文件。我忘记了什么或者如何检查?

1 个答案:

答案 0 :(得分:5)

我设法让它工作,解决方案没有建立到代码中的应有错误。无论如何这是解决方案。

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();
  }
}