1

我想在 csproj 文件中以编程方式修改调试和发布配置的属性值。我是否必须定义 4 个字典才能做到这一点?

示例:假设我想将 Debug 和 Release 配置的“WarningLevel”属性(“x86”和“AnyCPU”的值将保持不变)更改为 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????

我确信有更好的方法。任何建议将不胜感激。

提前致谢...

4

1 回答 1

3

您可以使用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"));
于 2012-08-22T21:33:55.343 回答