none
How to build a solution within C# (.NET 4.0) ?

    Question

  •  

    With .NET 3.5 I could build a solution or a project with the following code:

    Engine e = new Engine();

    BuildPropertyGroup properties = new BuildPropertyGroup();

    properties.SetProperty("Configuration", "Release");

    properties.SetProperty("Platform", "Any CPU");

    bool ok = e.BuildProjectFile(solutionOrProjectFileName, new string[] { "rebuild" }, properties);

     

    With .NET 4.0 the class Engine has been deprecated.

    With the following new way I can build a project but no solution:

    var pc = new ProjectCollection();

    pc.SetGlobalProperty("Configuration", "Release");

    pc.SetGlobalProperty("Platform", "Any CPU");

    BuildManager.DefaultBuildManager.Build(new BuildParameters(pc), new BuildRequestData(new ProjectInstance(projectFileName), new string[] { "Rebuild" }));

     

    HOW CAN I BUILD A COMPLETE SOLUTION???

    Thursday, May 27, 2010 7:42 AM

Answers

  • Hi,

    As I consult some experts(Cliff and Dan), I finally got an answer.

    We need set BuildRequestData Properly. Here is the sample code that works as expected.

            static void Main(string[] args)

            {

                string projectFileName = @"...\ConsoleApplication3\ConsoleApplication3.sln";

                ProjectCollection pc = new ProjectCollection();

                Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();

                GlobalProperty.Add("Configuration", "Debug");

                GlobalProperty.Add("Platform", "x86");

     

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

     

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

            }

    Hope this helps!

    Thanks

    Chao

    • Marked as answer by delauf Wednesday, June 02, 2010 7:29 AM
    Wednesday, June 02, 2010 3:28 AM

All replies

  • Hi,

    As I consult some experts(Cliff and Dan), I finally got an answer.

    We need set BuildRequestData Properly. Here is the sample code that works as expected.

            static void Main(string[] args)

            {

                string projectFileName = @"...\ConsoleApplication3\ConsoleApplication3.sln";

                ProjectCollection pc = new ProjectCollection();

                Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();

                GlobalProperty.Add("Configuration", "Debug");

                GlobalProperty.Add("Platform", "x86");

     

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

     

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

            }

    Hope this helps!

    Thanks

    Chao

    • Marked as answer by delauf Wednesday, June 02, 2010 7:29 AM
    Wednesday, June 02, 2010 3:28 AM
  • This works well.

    Thanks a lot!

    Wednesday, June 02, 2010 7:31 AM
  • Hi,

    I am trying to build C++ solution using this technique. Can you please provide an example on how to do that? I am also not sure how to get logger message, so I can see what error is causing the failure.

     

    Thanks!

    Larry

    Friday, June 10, 2011 8:02 PM
  • This is working for me for the most part.

    However, in .Net 2.0, I was using

    Engine engine = new Engine();
    BuildPropertyGroup bpg = new BuildPropertyGroup();
    bpg.SetProperty("Configuration", "Release");
    bpg.SetProperty("OutputDir", "bin\\Release");
    // Build a project file (.csproj or .sln)
    bool success = engine.BuildProjectFile(strCSProject, null, bpg)

     

    As you can see, I was passing in a null for the Targets parameter.

    What value do I need to pass in to the BuildRequestData constructor in order to build the same target(s)?

    Friday, September 09, 2011 8:57 PM
  • I realize that this is an old thread and all, but is there anyway, using the above approach to use the /maxcpucount config flag in this setup? I've tried to use
    MaxNodeCount property in the BuildParameters class

    MaxNodeCount = Environment.ProcessorCount; (on my current system this would result in 8)

    But this doesn't have any apparent influence on my build times, nor does it seem to have any impact on the CPU load.

    Hope you are able to help me out

     

     

     
    Thursday, October 25, 2012 9:41 AM
  • The solution you provided for building a solution file through C#(.Net 4.0) works fine. But, I have a situation here where I would like to specify the version number (Major.Minor.Build.Revision) for the build.

    I don't want to go by the approach of setting it in AssemblyInfo.cs but rather set it through code. Any thoughts?

    Wednesday, November 28, 2012 6:25 AM
  • @nsf20 - I don't know for sure, but I think the version number must be set in AssemblyInfo.cs... perhaps there is a tool out there or a means of automatically updating that file via code, but I am not certain.
    Wednesday, November 28, 2012 3:33 PM
  • Hi,

    This works fine if the reffered dll is anywhere else except bin folder. What might be the reason for this? The solution which I am building gets built successfuly from visual studio but fails when built by the above code.

    Thanks.

    Tuesday, July 30, 2013 9:59 AM