问题
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