Search code examples
c#nugetnuget-package

Is it possible to automatically update NuGet packages after a WinForms installation?


Let's say I have a Windows Forms application with a few NuGet packages that are important and need to be kept up-to date.

Is it somehow possible to update NuGet packages programmatically from a non-development environment? With a non-development environment I mean a random user that is running the WinForms application (having it installed on their PC).

I've read some things about using nuget.exe, but updating the NuGet packages should result in .dll files to be placed in the installation folder.


Solution

  • You can do that, but you should not do it. NuGet packages are development dependencies and not meant to be updated arbitrarily in an already compiled application or at the customer site, because:

    • You cannot be sure that your application will work with the updated assemblies, since they may introduce changes that will lead to crashes or unexpected behavior at runtime.
    • NuGet packages not only include assemblies, but also build scripts and resources that may depend on MS Build or other tools that run in your development environment to be deployed or even included in your own assembly, like embedded resources.
    • Packages have dependencies to assemblies and other packages. You will need to update the dependencies, too, and there is a lot of potential to break anything with it.
    • You would need to include the NuGet CLI executable when shipping your application and your customer would need to a allow for pulling and installing packages.
    • Installing packages without testing them first may harm the quality of your application and could also introduce security issues. Remember that you may be dealing with executables from a potentially public package source.

    That being said, do not do it. Instead, follow a responsible software development cycle, where you update packages and test your application thoroughly before delivery and provide frequent updates of you whole application to your customers.

    Nevertheless, for educational purposes, you can install packages locally with NuGet CLI tools, in this example nuget.exe. You need to specify the package identifier, the output directory and the framework, like net472 for .NET Framework 4.7.2. This will extract the contents, as well as the package itself to the output folder in the package folder structure that will not match your target directory structure. From there, you would need to copy the assets that you need into your install directory e.g. with a copy script. Apart from not being the right thing to do, this is very cumbersome and most likely deemed to fail.

    nuget install <PackageId> -OutputDirectory <OutputDirectory> -Framework <Framework>