Create a visual studio solution from code

I am writing a console application to get a solution from tfs server, create it and publish it on iis, but I'm stuck in creating ...

I found this code that works like a charm

public static void BuildProject()
{
    string solutionPath = Path.Combine(@"C:\MySolution\Common\Common.csproj");

    List<ILogger> loggers = new List<ILogger>();
    loggers.Add(new ConsoleLogger());
    var projectCollection = new ProjectCollection();
    projectCollection.RegisterLoggers(loggers);
    var project = projectCollection.LoadProject(solutionPath);
    try
    {
        project.Build();
    }
    finally
    {
        projectCollection.UnregisterAllLoggers();
    }
}

but my solution is quite large and contains several projects that depend on each other (for example, project A has a link to project B)

How to get the right order to create each project? is there a way to build a complete solution from a .sln file?

+4
source share
1 answer

Try the following code to download the solution and compile it:

string projectFilePath = Path.Combine(@"c:\solutions\App\app.sln");

ProjectCollection pc = new ProjectCollection();

// THERE ARE A LOT OF PROPERTIES HERE, THESE MAP TO THE MSBUILD CLI PROPERTIES
Dictionary<string, string> globalProperty = new Dictionary<string, string>();
globalProperty.Add("OutputPath", @"c:\temp");

BuildParameters bp = new BuildParameters(pc);
BuildRequestData buildRequest = new BuildRequestData(projectFilePath, globalProperty, "4.0", new string[] { "Build" }, null);
// THIS IS WHERE THE MAGIC HAPPENS - IN PROCESS MSBUILD
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, buildRequest);
// A SIMPLE WAY TO CHECK THE RESULT
if (buildResult.OverallResult == BuildResultCode.Success)
{              
    //...
}
+7
source

Source: https://habr.com/ru/post/1530436/


All Articles