[英]Update NuGet packages programmatically
I've seen many questions and examples about installing NuGet
packages, but not one about updating packages. 我已经看到了许多有关安装
NuGet
软件包的问题和示例,但没有一个有关更新软件包的问题和示例。
What's the best way to update NuGet
packages? 更新
NuGet
软件包的最佳方法是什么? I'm trying to do this from a ASP.NET MVC
application to update packages in a UWP
project which has projec.json
for packages references. 我正在尝试从
ASP.NET MVC
应用程序执行此操作,以更新具有projec.json
作为包引用的UWP
项目中的包。
Do I need to update .csproj
and project.json
files as well? 我还需要更新
.csproj
和project.json
文件吗?
After struggling with NuGet.VisualStudio
and NuGet.Core
packages and spending so much time, I've found out that the best approach is to use NuGet CLI and .NET Process
object. 在努力使用
NuGet.VisualStudio
和NuGet.Core
程序包并花费大量时间之后,我发现最好的方法是使用NuGet CLI和.NET Process
对象。 After downloading nuget.exe
this is how to update packages: 下载
nuget.exe
这是更新软件包的方法:
var updateOutput = new List<string>();
var updateError = new List<string>();
var updateProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "the path to nuget.exe file",
Arguments = "update " + "project path including .csproj file",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
updateProcess.Start();
while (!updateProcess.StandardOutput.EndOfStream)
{
updateOutput.Add(updateProcess.StandardOutput.ReadLine());
}
while (!updateProcess.StandardError.EndOfStream)
{
updateError.Add(updateProcess.StandardError.ReadLine());
}
After that what you do with updateOutput
and updateError
is your decision based on your needs. 之后,您对
updateOutput
和updateError
所做的就是根据您的需求做出的决定。
Note: In my case I had project.json
for package configuration and nuget.exe
needed packages.config
file. 注意:在我的情况下,我使用
project.json
进行程序包配置,而nuget.exe
需要使用packages.config
文件。 So, I created a temporary packages.config
file and after updating packages I deleted it. 因此,我创建了一个临时的
packages.config
文件,并在更新软件包后将其删除。 Like this: 像这样:
var ProjectPath = "the path to project folder";
var input = new StringBuilder();
input.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
input.AppendLine("<packages>");
using (var r = new StreamReader(ProjectPath + @"\project.json"))
{
var json = r.ReadToEnd();
dynamic array = JsonConvert.DeserializeObject(json);
foreach (var item in array.dependencies)
{
var xmlNode = item.ToString().Split(':');
input.AppendLine("<package id=" + xmlNode[0] + " version=" + xmlNode[1] + " />");
}
}
input.AppendLine("</packages>");
var doc = new XmlDocument();
doc.LoadXml(input.ToString());
doc.Save(ProjectPath + @"\packages.config");
// After updating packages
File.Delete(ProjectPath + @"\packages.config");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.