【问题标题】:How can I add a Nuget package to a project in my Visual Studio extension?如何将 Nuget 包添加到 Visual Studio 扩展中的项目?
【发布时间】:2019-02-06 19:33:22
【问题描述】:

我正在构建一个 Visual Studio 扩展,并且我想在用户单击一个按钮时添加一个特定的 Nuget 包。我环顾了 Visual Studio SDK,似乎没有关于如何执行此操作的文档。有什么想法吗?

【问题讨论】:

    标签: visual-studio nuget visual-studio-extensions


    【解决方案1】:

    有一个 IVsPackageInstaller 可用于从 Visual Studio 中安装 NuGet 包。

    安装 NuGet.VisualStudio NuGet 包,然后使用 MEF 访问它。

    //Using the Import attribute
    [Import(typeof(IVsPackageInstaller2))]
    public IVsPackageInstaller2 packageInstaller;
    packageInstaller.InstallLatestPackage(null, currentProject,
        "Newtonsoft.Json", false, false);
    
    //Using the IComponentModel service
    var componentModel = (IComponentModel)GetService(typeof(SComponentModel));
    IVsPackageInstallerServices installerServices =
        componentModel.GetService<IVsPackageInstallerServices>();
    
    var installedPackages = installerServices.GetInstalledPackages();
    

    以上内容摘自 NuGet 网站上的文档:

    https://docs.microsoft.com/en-us/nuget/visual-studio-extensibility/nuget-api-in-visual-studio

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多