gpt4 book ai didi

c# - 向项目添加 Postbuild 事件

转载 作者:太空狗 更新时间:2023-10-30 00:51:42 24 4
gpt4 key购买 nike

当我生成一个 C# 项目(csproj 文件)并对其进行编译时,msbuild 不知何故无法识别预生成和生成后事件中的变量 $(ConfigurationName)$(ProjectDir)(以及其他)。

当我手动将生成的 .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);
}

我在通过msbuild运行生成的项目时得到的错误是这样的:

The command "copy "app.config." "\.dll.config"" exited with code 1

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

将构建事件添加到 .csproj 文件以使其在生成的 Import 中的 XML 元素之后结束的最佳方法是什么?

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

示例项目:

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();
}
}

重现步骤

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

最佳答案

var propertyGroupElement = project.Xml.CreatePropertyGroupElement();
project.Xml.AppendChild(propertyGroupElement);
propertyGroupElement.AddProperty(PreBuildEventFixture, PreBuildEvent);
propertyGroupElement.AddProperty(PostBuildEventFixture, PostBuildEvent);

关于c# - 向项目添加 Postbuild 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25567949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com