Josh Redpath
Josh Redpath

Reputation: 33

How can I programmactically call Package Manager Console commands in Visual Studios, C# like Get-Package on a NuGet feed?

I want to get the response from "Get-Package -Source "nuget feed" -ListAvailable" that you can get manually in the Package Manager Console automatically in my program. Is there a way to do this programmatically?

Upvotes: 2

Views: 2040

Answers (2)

user9564960
user9564960

Reputation:

1)Add an assembly reference for 'EnvDTE' to your project like this:

enter image description here

2)Use this code to call the PMC:

                var objDte = System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE") as DTE;
            var script = "update-database";
            objDte.ExecuteCommand("View.PackageManagerConsole", script);

Change the script content to the command that you want to execute in the PMC (I needed to update the database using EF)

Answer inspired from this :

https://www.codeproject.com/Questions/688629/How-to-access-package-manager-console-through-csha

Upvotes: 2

Harikrishna Menon A
Harikrishna Menon A

Reputation: 455

Instead of using Package Manager console, have you considered using Process.Start() on nuget.exe and piping the results to log or reading from the console. nuget.exe has similar commands that will enable you to get the results you want.

PMC commands are only available within the context of the package manager console and not just any powershell window. While there could be a convoluted way of using VS extensibility to get to the results, I would recommend that you look at the nuget.exe approach.

Upvotes: 0

Related Questions