Run NuGet command from C # code

I am trying to find a way to call Install-Package myPackagedirectly from C # code.

I tried to do this using powershell in C #, but I am not very familiar with it.

Can someone give me an example of how to do this?

+3
source share
2 answers

You can try something like this to execute powershell command

Namespace : System.Management.Automation

Build : System.Management.Automation (in system.management.automation.dll)

    private static void RunPowershell(string command)
    {

        var powerShell = PowerShell.Create();
        powerShell.AddCommand(command);
        powerShell.Invoke();

    }
+1
source

You need to add the NuGet module to the PowerShell session. After installing NuGet, you can run Get-Module and see what is available

PM> get-module | fl *


ExportedCommands    : {Get-Package, Update-Package, Register-TabExpansion, Get-Project...}
Name                : NuGet
Path                : C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft Corporation\NuGet Package Manager\1.1.229.160\Scripts\nuget.psm1
Description         : NuGet PowerShell module used for the Package Manager Console
Guid                : 76e6f9c4-7045-44c0-a557-17fab0835c12
ModuleBase          : C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft Corporation\NuGet Package Manager\1.1.229.160
PrivateData         : 
Version             : 1.1.229.160
ModuleType          : Script
AccessMode          : ReadWrite
ExportedFunctions   : {[NugetTabExpansion, NugetTabExpansion], [Register-TabExpansion, Register-TabExpansion]}
ExportedCmdlets     : {[Add-BindingRedirect, Add-BindingRedirect], [Get-Package, Get-Package], [Get-Project, Get-Project], [Install-Package, Install-Package]...}
NestedModules       : {NuGet.Cmdlets}
RequiredModules     : {}
ExportedVariables   : {}
ExportedAliases     : {}
SessionState        : System.Management.Automation.SessionState
OnRemove            : 
ExportedFormatFiles : {C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft Corporation\NuGet Package Manager\1.1.229.160\Scripts\NuGet.Format.ps1xml}
ExportedTypeFiles   : {C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft Corporation\NuGet Package Manager\1.1.229.160\Scripts\NuGet.Types.ps1xml}

, PSM1

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft Corporation\NuGet Package Manager\1.1.229.160\Scripts\nuget.psm1

PSM1, NuGet.

0

Source: https://habr.com/ru/post/1793160/


All Articles