简体   繁体   English

以编程方式更新NuGet软件包

[英]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? 我还需要更新.csprojproject.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.VisualStudioNuGet.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. 之后,您对updateOutputupdateError所做的就是根据您的需求做出的决定。

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.

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