向项目添加 Postbuild 事件

Alf*_*ons 4 c# msbuild msbuild-propertygroup microsoft.build

当我生成一个 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"" exited with code 1
Run Code Online (Sandbox Code Playgroud)

然后当我手动编辑.csproj文件(使用记事本或其他文本编辑器),剪切 pre-and postbuild 事件,并将其粘贴到<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />元素下方时,然后 msbuild 会.csproj很好地构建生成的文件。

将构建事件添加到.csproj文件中以便它在结果中的Import元素之后结束的最佳方法是什么XML

显然,我目前[ProjectPropertyGroupElement][1]通过从Microsoft.Build.Evaluation.ProjectXml属性的AddPropertyGroup请求它的使用方式不是。

示例项目:

using System.IO;
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;

class Program
{
    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).exe.config\" \r\n attrib -R \"$(ProjectPath)\"";

    private const string ProjectFile = @"C:\test\TestProject\TestProject.csproj";

    static void Main(string[] args)
    {
        if (!File.Exists(ProjectFile))
            throw new FileNotFoundException("ProjectFile not found");

        ProjectCollection collection = new ProjectCollection();
        Project project = collection.LoadProject(ProjectFile);

        ProjectPropertyGroupElement propertyGroupElement = project.Xml.AddPropertyGroup();
        propertyGroupElement.AddProperty(PreBuildEventFixture, PreBuildEvent);
        propertyGroupElement.AddProperty(PostBuildEventFixture, PostBuildEvent);
        project.Save();
        collection.UnloadAllProjects();
    }
}
Run Code Online (Sandbox Code Playgroud)

重现步骤

  • 创建一个新项目
  • 手动添加应与 app.debug 文件不同的 app.config.debug 文件
  • 添加构建后事件: copy "$(ProjectDir)app.config.$(ConfigurationName)" "$(TargetDir)\$(ProjectName).exe.config
  • 查看是否应用了项目构建和正确的配置文件
  • 使用记事本删除构建前和构建后事件(以免留下任何痕迹)
  • 运行示例项目
  • 重新加载并构建您创建的项目。
  • 输出窗口现在会说 The system cannot find the file specified.

Ily*_*kov 5

var propertyGroupElement = project.Xml.CreatePropertyGroupElement();
project.Xml.AppendChild(propertyGroupElement);
propertyGroupElement.AddProperty(PreBuildEventFixture, PreBuildEvent);
propertyGroupElement.AddProperty(PostBuildEventFixture, PostBuildEvent);
Run Code Online (Sandbox Code Playgroud)