从c#代码执行NuGet命令

时间:2011-02-18 22:43:05

标签: c# visual-studio-2010 powershell nuget

我正试图找到一种从C#代码调用Install-Package myPackage权限的方法。

我试过在c#中使用powershell这样做,但我对它不是很熟悉。

有人能举例说明怎么做吗?

2 个答案:

答案 0 :(得分:1)

你可以尝试这样的东西来执行powershell命令

  

<强>命名空间:   System.Management.Automation

     

<强>装配:   System.Management.Automation(in   system.management.automation.dll)

    private static void RunPowershell(string command)
    {

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

    }

答案 1 :(得分:0)

您需要在PowerShell会话中添加NuGet模块。安装NuGet后,您可以运行Get-Module并查看可用的内容

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命令。