小编Alf*_*ons的帖子

向项目添加 Postbuild 事件

当我生成一个 C# 项目(csproj文件)然后编译它时,msbuild不知何故无法识别变量$(ConfigurationName)并且$(ProjectDir)pre- 和 postbuild 事件中(和其他)。

当我手动移动生成的构建前和构建后事件配置时 .csproj文件进一步向下移动时,msbuild 会正确识别这些变量。

将 buildevent 添加到项目中是我在保存项目之前做的最后一件事。

这是我添加它的方式:

using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;

private const string PreBuildEventFixture = "PreBuildEvent";
private const string PostBuildEventFixture = "PostBuildEvent";
private const string PreBuildEvent = "attrib -R \"$(ProjectDir)app.config\"";
private const string PostBuildEvent = "copy \"$(ProjectDir)app.config.$(ConfigurationName)\" \"$(TargetDir)\\$(ProjectName).dll.config\" \r\n attrib -R \"$(ProjectPath)\"";

public void AddBuildEvents(Project project)
{
    ProjectPropertyGroupElement propertyGroupElement = project.Xml.AddPropertyGroup();
    propertyGroupElement.AddProperty(PreBuildEventFixture, PreBuildEvent);
    propertyGroupElement.AddProperty(PostBuildEventFixture, PostBuildEvent);
}
Run Code Online (Sandbox Code Playgroud)

通过 msbuild 运行生成的项目时出现的错误是:

The command "copy "app.config." "\.dll.config"" …
Run Code Online (Sandbox Code Playgroud)

c# msbuild msbuild-propertygroup microsoft.build

4
推荐指数
1
解决办法
2681
查看次数