aromore
aromore

Reputation: 1077

Modify property values for Debug and Release Configuration without defining respective dictionaries

I want to modify property values of Both Debug and Release Configurations programatically in the csproj file. Do I have to define 4 dictionaries to do that?

Example: Lets say I want to change the "WarningLevel" property of the Debug and Release configurations (values will remain the same for "x86" and "AnyCPU") to WarningLevel = 4.

var globalPropertiesReleaseX86 = new Dictionary<string, string> { { "Configuration", "Release" }, { "Platform", "x86" } };
var globalPropertiesDebugX86 = new Dictionary<string, string> { { "Configuration", "Debug" }, { "Platform", "x86" } };
var globalPropertiesReleaseAnyCPU = new Dictionary<string, string> { { "Configuration", "Debug" }, { "Platform", "x86" } };
var globalPropertiesDebugAnyCpu = new Dictionary<string, string> { { "Configuration", "Debug" }, { "Platform", "x86" } };

Project projectReleaseX86 = new ProjectCollection(globalPropertiesReleaseX86).LoadProject(filePath);
projectReleaseX86.SetProperty("WarningLevel", "4");
...and so on????

I am sure there is a better approach. Any suggestions would be much appreciated.

Thanks in advance...

Upvotes: 1

Views: 195

Answers (1)

Pavel Bakshy
Pavel Bakshy

Reputation: 7747

You can use ProjectRootElement:

var project = ProjectRootElement.Open(fileName);
project.PropertyGroups.Where(x => x.Condition.Contains("x86") || 
                                  x.Condition.Contains("AnyCPU"))
    .ToList().ForEach(x => x.AddProperty("WarningLevel", "4"));

Upvotes: 3

Related Questions