Programmatically adding and editing the Targets in a Visual Studio Project File

瘦欲@ 提交于 2019-12-13 14:33:20

问题


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>

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.

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.

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


回答1:


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.



来源:https://stackoverflow.com/questions/4511344/programmatically-adding-and-editing-the-targets-in-a-visual-studio-project-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!