0

I've seen many questions and examples about installing NuGet packages, but not one about updating packages.

What's the best way to update NuGet packages? 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.

Do I need to update .csproj and project.json files as well?

Mo Sadeghipour
  • 489
  • 8
  • 25
  • https://stackoverflow.com/questions/6876732/how-do-i-get-nuget-to-install-update-all-the-packages-in-the-packages-config There is some data about Update, restore packages, etc.. please check if this help you. – Thiago Loureiro Aug 22 '17 at 09:05
  • Or you can use tools to do the trick , like https://github.com/davidfowl/NuGetPowerTools – Thiago Loureiro Aug 22 '17 at 09:06
  • Thanks, but none of them were useful. Notice that I'm trying to this programmatically (from C# code) – Mo Sadeghipour Aug 22 '17 at 09:21
  • couldn't you run the nuget cli via command-line in code? e.g. using Process https://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx – D Hansen Aug 22 '17 at 11:56
  • I could, but after it starts, there's no results, meaning I can't know if anything went wrong or did it succeed, etc – Mo Sadeghipour Aug 23 '17 at 04:00

1 Answers1

2

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. After downloading nuget.exe this is how to update packages:

            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.

Note: In my case I had project.json for package configuration and nuget.exe needed packages.config file. So, I created a temporary packages.config file and after updating packages I deleted it. 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");
Mo Sadeghipour
  • 489
  • 8
  • 25