I'm trying to find a way to call an Install-Package myPackage
right from C# code.
I've tried to do so using powershell in c# but I'm not very familiar with it.
Can anyone give me an example of how to do so?
You could try something like this to execute powershell command
Namespace : System.Management.Automation
Assembly : System.Management.Automation (in system.management.automation.dll)
private static void RunPowershell(string command)
{
var powerShell = PowerShell.Create();
powerShell.AddCommand(command);
powerShell.Invoke();
}
You need to add the NuGet Module in your 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}
Notice the path to the PSM1 file is here
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft Corporation\NuGet Package Manager\1.1.229.160\Scripts\nuget.psm1
if you import this PSM1 file, you should be able to execute your NuGet commands.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.