0

I have a visual studio solution file containing multiple projects.

For a given solution configuration, say for example Dev, few of the projects are using Debug configuration. I want all projects to use Dev configuration for the Dev solution configuration.

Instead of using Configuration Manager and update each project's configuration for all possible platform option (x64, Any CPU, Mixed Platforms), I want to be able to automate this. It is very tedious using Configuration Manager to update this for all solution configuration for all projects.

Because I have more than 5 solution configuration and there are more than 20 projects in the solution.

Editing the solution file in a notepad++ would help I guess. I am yet to try this. But is this the only way?

Update:

I have drafted a simple PS script to achieve this using Powershell to update the sln file, for a single sln configuration. But looking for alternate ways as well.

$content = Get-Content "E:\SlnFolder\SolutionFile.sln" | ForEach-Object {

$line = $_

if ($line -match "(^\s*{[a-zA-Z0-9]+-[a-zA-Z0-9]+-[a-zA-Z0-9]+-[a-zA-Z0-9]+-[a-zA-Z0-9]+}.Dev\|[a-zA-Z0-9\.\s]+ = )([a-zA-Z\s]+)")
{

$line -replace "(^\s*{[a-zA-Z0-9]+-[a-zA-Z0-9]+-[a-zA-Z0-9]+-[a-zA-Z0-9]+-[a-zA-Z0-9]+}.Dev\|[a-zA-Z0-9\.\s]+ = )([a-zA-Z\s]+)",'$1Dev'

}
else
{
$line
}

} | Set-Content -Path "E:\SlnFolder\SolutionFile_Updated.sln"

There is enough room to polish this script. Will check that.

  • 1
    Alternative is to edit the files via Microsoft.Build.BuildEngine, see e.g. http://stackoverflow.com/questions/7887538/modify-programatically-csproj-files#8492560 – stijn Dec 09 '16 at 08:19
  • @stijn - The answer discussed at the thread you have given is now obsolete. I have found another thread [http://stackoverflow.com/questions/707107/parsing-visual-studio-solution-files] where there is an answer for the newer way of using the Microsoft.Build.Construction.SolutionFile.Parse() method with VS 2015. The MSBuild path is C:\Program Files (x86)\Reference Assemblies\Microsoft\MSBuild\v14.0\Microsoft.Build.dll – Baskar Lingam Ramachandran Dec 09 '16 at 10:49
  • I'm not sure it's really obsolete? Note it's for altering *projects*, the question you link to is about *solutions*. Though for your case you probably need both. – stijn Dec 09 '16 at 15:53

1 Answers1

0

Yes, You can retrieve all projects AbsolutePath by using SolutionFile.Parse method, you can use Microsoft.Build.Evaluation.Project class to set related Property. Like this:

Please add a extensions reference named Microsoft.Build

string solutionPath = @"D:\Project\Msbuild\App1\App1.sln";
var solutionFile = SolutionFile.Parse(solutionPath);
foreach (var item in solutionFile.ProjectsInOrder)
{
var project = ProjectCollection.GlobalProjectCollection.LoadProject(item.AbsolutePath);
project.SetGlobalProperty("Configuration", "Debug");
project.Build();
}
Zhanglong Wu - MSFT
  • 1,652
  • 1
  • 7
  • 8
  • @ Baskar, what about this issue? Could you get useful information from Cole's reply? If it is helpful for you, remember mark it as the answer:) Have a nice day! – Jack Zhai Dec 14 '16 at 10:14
  • @JackZhai-MSFT - I want to be able to just update the sln file without loading the corresponding project. Is this possible? Basically I am looking for the API for Configuration Manager. In Visual Studio we can open the Configuration Manager and update configuration of projects for a sln configuration. This option is tedious, if I have 15 projects and more than 5 or 6 sln conifgurations (Like Dev, Test, Prod, UAT, so on). Looking for a way to be able to just update the sln file directly using an API for Configuration Manager. Using regex I could update sln directly. But I rely on API rather. – Baskar Lingam Ramachandran Dec 15 '16 at 09:48
  • you can write a custom target file, and import the target file on all of your project file. for more information, please refer to: http://stackoverflow.com/questions/2293249/msbuild-set-properties-for-solution – Zhanglong Wu - MSFT Dec 15 '16 at 11:07