如何以编程方式安装NuGet包? [英] How to programmatically install a NuGet package?

查看:57
本文介绍了如何以编程方式安装NuGet包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式将NuGet包安装到项目中,并更新.csproj文件和packages.config文件。

现在我可以从Nuget添加DLL to package.config,但我无法更新.cs proj文件。



 string packageID =Newtonsoft.json; 
var sourceUri = new Uri(https://packages.nuget.org/api/v2);

//返回一个IPackage
var package = GetNugetPackage(packageID,sourceUri);

IPackageRepository sourceRepository = PackageRepositoryFactory.Default.CreateRepository(sourceUri.ToString());

string packagesPath =../../TestFiles/packages;
PackageManager packageManager = new PackageManager(sourceRepository,packagesPath);

packageManager.InstallPackage(packageID,SemanticVersion.Parse(package.Version.ToFullString()));

// ----更新'packages.config'文件----
var packageReferenceFile = new PackageReferenceFile(../../ TestFiles / packages.config) ;

//获取要添加的当前项目的目标框架 - > package.config文件中的targetframework =net452属性
var currentTargetFw = Assembly.GetExecutingAssembly()。GetCustomAttributes(typeof(TargetFrameworkAttribute),false);
var targetFrameworkAttribute =((TargetFrameworkAttribute [])currentTargetFw).FirstOrDefault();

//更新packages.config文件
packageReferenceFile.AddEntry(package.GetFullName(),SemanticVersion.Parse(package.Version.ToFullString()),false,new FrameworkName(targetFrameworkAttribute。 FrameworkName));





我尝试过:



StackOverflow:如何以编程方式安装NuGet包? [ ^ ]

解决方案

也许其中一个 YouTube视频 [ ^ ]将为您解答您的问题。



编辑:通过您提供的链接,Thomas为他提供了一个很好的解决方案a href =http://stackoverflow.com/a/41895490>答案 [ ^

I want to programmatically install a NuGet package to a project, and update the .csproj file, and the packages.config file.

Right now i can add DLL from Nuget to package.config,but i couldn't update .cs proj file.

string packageID = "Newtonsoft.json";
var sourceUri = new Uri("https://packages.nuget.org/api/v2");

// Return an IPackage
var package = GetNugetPackage(packageID, sourceUri);

IPackageRepository sourceRepository = PackageRepositoryFactory.Default.CreateRepository(sourceUri.ToString());

string packagesPath = "../../TestFiles/packages";
PackageManager packageManager = new PackageManager(sourceRepository, packagesPath);

packageManager.InstallPackage(packageID, SemanticVersion.Parse(package.Version.ToFullString()));

// ---- Update the ‘packages.config’ file ----
var packageReferenceFile = new PackageReferenceFile("../../TestFiles/packages.config");

// Get the target framework of the current project to add --> targetframework="net452" attribute in the package.config file
var currentTargetFw = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(TargetFrameworkAttribute), false);
var targetFrameworkAttribute = ((TargetFrameworkAttribute[])currentTargetFw).FirstOrDefault();

// Update the packages.config file    
packageReferenceFile.AddEntry(package.GetFullName(), SemanticVersion.Parse(package.Version.ToFullString()), false, new FrameworkName(targetFrameworkAttribute.FrameworkName));



What I have tried:

StackOverflow: How to programmatically install a NuGet package?[^]

解决方案

Maybe one of these YouTube Videos[^] will answer your question for you.

Edit: With the link provided by you, Thomas gives a great solution in his Answer[^]


这篇关于如何以编程方式安装NuGet包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆