以编程方式添加和编辑的目标在Visual Studio项目文件编辑、目标、方式、文件

2023-09-05 01:59:09 作者:Await[期待]

我想下面的目标编程方式添加到我的Visual Studio项目文件。

I am trying to programmatically add the following Target to my Visual Studio Project File.

<Target Name="PostBuildEvent" Condition="'$(PostBuildEvent)'!=''" DependsOnTargets="$(PostBuildEventDependsOn)">
      <Exec WorkingDirectory="$(OutDir)" Command="$(PostBuildEvent)" IgnoreExitCode="true" />
</Target>

我是pretty的确认 IVsBuildPropertyStorage IVsBuildPropertyStorage2 将无法工作,因为需要一个项目项又名一个节点从&LT; ItemGroup&gt;在项目文件元素。我想这样做的是添加上面的xml外&LT; ItemGroup&GT; 元素

I am pretty sure that IVsBuildPropertyStorage or IVsBuildPropertyStorage2 will not work because the require a ProjectItem aka a node from the <ItemGroup> element in the project file. What I would like to do is add the above xml outside the <ItemGroup> element.

我试图使用Visual Studio SDK来做到这一点,文档和放大器;做这个确切的事情的例子似乎缺乏。作为这篇文章的时候,我无法找到这样做的方式。我的真的想避免装载了项目文件(在这种情况下Test.csproj)作为的XDocument XmlDocument的键,将目标元素节点的方式。看来杂乱,并可能导致更多的问题。

I am trying to use the Visual Studio SDK to do this and the documentation & examples on doing this exact thing seems to be lacking. As of the time of this post I am unable to find a way of doing this. I would really like to avoid loading up the project file (in this case Test.csproj) as an XDocument or an XmlDocument and adding the Target element node that way. It seems messy and could cause more problems.

作为一个额外的好处我也想IgnoreOnExit一个 PostBuildEvent 元素,但我pretty的肯定,这是不可能的 PostBuildEvent 元素

As an added bonus I would also like to IgnoreOnExit a single PostBuildEvent element, but I am pretty sure that this is not possible with the PostBuildEvent element

推荐答案

尝试使用 AddTarget 上的 ProjectRootElement 在 Microsoft.Build.Construction 命名空间中的类。该命名空间用于读取和写入的MSBuild文件。

Try using the AddTarget method on the ProjectRootElement class in the Microsoft.Build.Construction namespace. That namespace is used to read and write MSBuild files.