0
我見過很多關於安裝NuGet
軟件包的問題和示例,但沒有關於更新軟件包的問題和示例。以編程方式更新NuGet軟件包
什麼是更新NuGet
軟件包的最佳方式?我試圖從ASP.NET MVC
應用程序中執行此操作,以更新UWP
項目中的軟件包,該項目的軟件包引用爲projec.json
。
我需要更新.csproj
和project.json
文件嗎?
我見過很多關於安裝NuGet
軟件包的問題和示例,但沒有關於更新軟件包的問題和示例。以編程方式更新NuGet軟件包
什麼是更新NuGet
軟件包的最佳方式?我試圖從ASP.NET MVC
應用程序中執行此操作,以更新UWP
項目中的軟件包,該項目的軟件包引用爲projec.json
。
我需要更新.csproj
和project.json
文件嗎?
經過與NuGet.VisualStudio
和NuGet.Core
包並花費這麼多時間掙扎後,我發現最好的方法是使用NuGet CLI和.NET Process
對象。下載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());
}
之後,你與updateOutput
和updateError
做的是根據您的需要你的決定。
注:對我來說,我有project.json
的包配置和nuget.exe
需要packages.config
文件。所以,我創建了一個臨時的packages.config
文件,並在更新軟件包之後刪除了它。像這樣:
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");
https://stackoverflow.com/questions/6876732/how-do-i-get-nuget-to-install-update-all-the-packages-in-the-packages-config有一些關於更新,恢復軟件包等的數據。請檢查這是否對你有幫助。 –
或者你可以使用工具來做伎倆,如https://github.com/davidfowl/NuGetPowerTools –
謝謝,但沒有一個是有用的。請注意,我試圖以編程方式(從C#代碼) –