首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Postbuild事件添加到项目

将Postbuild事件添加到项目
EN

Stack Overflow用户
提问于 2014-08-29 12:04:44
回答 2查看 3K关注 0票数 4

当我生成一个C#项目(csproj文件)并对其进行编译时,msbuild在构建前后事件中不知怎么不识别变量$(ConfigurationName)$(ProjectDir) (以及其他变量)。

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

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

我是这样加起来的:

代码语言:javascript
复制
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运行生成的项目时,我遇到的错误如下:

代码语言:javascript
复制
The command "copy "app.config." "\.dll.config"" exited with code 1

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

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

显然,我现在通过从[ProjectPropertyGroupElement][1]属性的AddPropertyGroup中请求它而使用Microsoft.Build.Evaluation.Project的方法不是。

示例项目:

代码语言:javascript
复制
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.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-03 09:25:30

代码语言:javascript
复制
var propertyGroupElement = project.Xml.CreatePropertyGroupElement();
project.Xml.AppendChild(propertyGroupElement);
propertyGroupElement.AddProperty(PreBuildEventFixture, PreBuildEvent);
propertyGroupElement.AddProperty(PostBuildEventFixture, PostBuildEvent);
票数 5
EN

Stack Overflow用户

发布于 2014-09-01 12:48:58

如果在实际构建项目之前添加了与项目相关的宏(构建项目包括添加引用),则不解析它们。不用使用$(ProjectName),可以使用解决方案变量(已经存在)构造路径,如下所示:

copy "$(SolutionDir)ProjectName\app.config.$(Configuration)" "$(SolutionDir)ProjectName\bin\$(Configuration)\ProjectName.dll.config"

注意,ProjectName是硬编码项目的实际名称,但是由于您正在生成一个项目,这应该很容易添加。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25567949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档