简体   繁体   English

使用VSPackage安装nuget软件包

[英]Install nuget package with a VSPackage

I'm trying to install a nuget package into a project that I am generating with a VSPackage. 我正在尝试将nuget软件包安装到使用VSPackage生成的项目中。 So far, I am able to create a solution from a project template: 到目前为止,我已经能够从项目模板创建解决方案:

Solution4 soln = (Solution4)ApplicationObject.Solution;

string prjPath = "C:\\MyProject";
string templatePath = soln.GetProjectTemplate(@"SomeProject\MyTemplate.vstemplate", "CSharp");

soln.AddFromTemplate(templatePath, prjPath, "New CSharp Project", false);

But now I need to be able to install a nuget package into that project as well. 但是现在我还需要能够在该项目中安装一个nuget包。 The package is located online, for example, https://somewhere.mydomain.com/nuget/feed , and has the ID PackageX . 该软件包位于在线位置,例如https://somewhere.mydomain.com/nuget/feed ,并且具有ID PackageX

You'll need to perform the same steps that " NuGet Package Restore " does, adding NuGet.exe, NuGet.config and NuGet.targets correctly to the solution. 您需要执行与“ NuGet软件包还原 ”相同的步骤,将NuGet.exe,NuGet.config和NuGet.targets正确添加到解决方案中。 To see how this changes things, diff two empty solutions where one restores NuGet packages and the other doesn't. 要了解这是如何改变的,请比较两个空解决方案,其中一个还原NuGet软件包,而另一个不还原。

To add your custom feed, you can add 要添加自定义供稿,您可以添加

  <packageSources>
    <add key="local" value="https://somwhere.mydomain.com/nuget/feed"/>
  </packageSources>

to NuGet.config under the <configuration> node. <configuration>节点下的NuGet.config。

Finally, add the package name and version to packages.config in the vcproj directory and you should be all set! 最后,将软件包名称和版本添加到vcproj目录中的packages.config中,您应该已经准备就绪!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM