0

I need to remove a reference from several projects in Visual Studio each time when I re/install/update a particular NuGet package that I cannot currently modify.

Is it possible to automate this process with PowerShell or a command?

It would also do if I just could execute it for each project by pasting it in the Package Manager Console instead of opening each one and deleting it manually.

Actually I need a similar functionality as the Uninstall-Package command but for a single reference. I had no luck looking for the source code of this command.

t3chb0t
  • 16,340
  • 13
  • 78
  • 118

1 Answers1

1

Somebody on SO already did that here with powershell, he gives the scripts to add or remove references to a csproj.

If you want to do that while your solution is opened in Visual Studio, you can also do it using a Visual Studio Add-in. Create an Add-in and add a reference to EnvDTE, Extensibility and VSLangProj.

Then here is some sample code to put in the Exec method to get you started:

foreach (Project project in _applicationObject.Solution.Projects)
{
    if (project.Object is VSLangProj.VSProject)
    {
        VSLangProj.VSProject vsproject = (VSLangProj.VSProject)project.Object;

        var reference = vsproject.References.Find("System.Xml");
        if (reference != null)
            reference.Remove();
    }
}
Community
  • 1
  • 1
Nicolas C
  • 752
  • 5
  • 18
  • Oh, probably if I had looked for a csproj I could have found it too :) but I thought it would be some _standard_ Visual Studio API. I'm quite surprised you actually need to modify the project file by yourself. – t3chb0t Aug 20 '15 at 16:06
  • Me too, that's why I suggested the add-in, this way it's a standard api and not some messing around in the csproj ;) – Nicolas C Aug 21 '15 at 08:01
  • Still experimenting... I'll let you know when it works and mark it as answered :-) – t3chb0t Aug 24 '15 at 10:43