Uninstalling .csproj edits

Jun 7, 2011 at 6:44 PM

My NuGet package adds a custom <Target> to the .csproj file during installation using some custom code in the Install.ps1.  I leverage some helper functions in David Fowler's NuGetPowerToys (e.g. Get-MSBuildProject).  It adds the target using syntax like this:


$target = $msbProject.Xml.AddTarget("MyCustomTarget")
$target.AfterTargets = "Build"
$task = $target.AddTask("MyTask")
$task.SetParameter("MyProp", '$(WebProjectOutputDir)')

This all works great.  However, now I am trying to write the Uninstall.ps1, and looking over the API for the Project class (the one returned by Get-MSBuildProject), I can't seem to find where it would support removing elements.  The API seems to only support Adding/Creating elements.  Am I missing something?  Any help would be appreciated.


Jun 13, 2011 at 4:10 PM

You need to find your Target in the list of targets (msbProject.Xml.Targets) then call Remove on it.

Jun 13, 2011 at 8:44 PM

Ah, that is exactly what I was looking for.  Don't know how I missed it.  Thanks!

Jun 13, 2011 at 9:56 PM

So I've tried calling the Remove method on msbProject.Xml.Targets and it throws saying "This collection is read-only".  Did you have a different Remove method in mind?

Jun 13, 2011 at 10:40 PM

I figured it out.  There is a RemoveChild method available.  For those of you following the thread, it ends up looking like this:

$target = $msbProject.Xml.Targets | 
                   where { $_.Name -eq "MyCustomTarget" }
if($target -ne $null)