WGet.NET is a WinGet wrapper library for .Net that allows you to easily install, update, uninstall and more, with the help of WinGet, from your application.
I created it for another project and then made a library out of it in the hope it might be useful to someone else too.
If you miss a feature or find a problem with the library, feel free to create an issue.
Version 4.0 is intended as a “health” update for the library and therefore includes changes to the structure, classes and functions.
This includes breaking changes to the usage of the library.
But fear not, no functionality is lost and all breaking changes are documented and provide information on how to change the implementation.
You can find the full migration documentation here.
This was done to improve maintainability and the experience of using the library, by improving the data structure, removing redundant or useless functions and making the implementation more consistent.
More information about version 4.0 can be found here.
https://basicx-strgv.github.io/WGet.NET/
- WinGet
- Get a list of installed packages
- Search installed packages
- Search packages
- Install packages
- Uninstall packages
- Upgrade packages
- Repair packages
- Get a list of upgradeable packages
- Export and Import packages
- Check if WinGet is installed
- Get the WinGet version number
- Get installed sources
- Add sources
- Remove sources
- Update sources
- Export and Import sources
- Reset sources
- Calculate file hash
- Export Settings
- Download package installers
- Manage pinned packages
- Access info of the WinGet installation
- Other
- Asynchronous Execution
You can get the NuGet package here: https://www.nuget.org/packages/WGet.NET/
WinGet needs to be installed on the system.
If WinGet is not installed on your system you can get it here: https://github.com/microsoft/winget-cli
The needed namespace is WGetNET (using WGetNET;
).
This namespace contains the three main classes that are used to perform actions or get information with winget, plus additional classes that are needed.
Exceptions are located in the WGetNET.Exceptions
namespace.
The WinGet class can be used to get information about WinGet itself.
This class is inherited by the WinGetPackageManager and WinGetSourceManager class.
Using this class to check if winget is installed could look like this:
WinGet winget = new WinGet();
if (winget.IsInstalled)
{
Console.WriteLine("WinGet is installed.");
}
else
{
Console.WriteLine("WinGet is NOT installed.");
}
The WinGetPackageManager class is used for everything that has to do with packages.
It can install, remove, upgrade, search, list, export and import packages.
The code for installing a package could look like this:
WinGetPackageManager packageManager = new WinGetPackageManager();
packageManager.InstallPackage("Git.Git");
The WinGetSourceManager class is used for everything that has to do with sources.
It can list, add, update, export, reset and remove sources.
To use the add, remove and reset functions, the process needs to have administrator privileges.
(WinGet can’t perform these actions without administrator privileges)
The code for adding a source could look like this:
WinGetSourceManager sourceManager = new WinGetSourceManager();
sourceManager.AddSource("msstore", "https://storeedgefd.dsx.mp.microsoft.com/v9.0", "Microsoft.Rest");
Using the WinGetPackageManager class you can use the GetInstalledPackages
capability to get the latest version of a package and then retrieve the version number from the AvailableVersionObject
property.
You would then be able to compare this to the current version of the package (VersionObject
property) and determine if you need to notify users of an available upgrade.
WinGetPackageManager packageManager = new WinGetPackageManager();
string packageId = "nkdAgility.AzureDevOpsMigrationTools";
WinGetPackage package = packageManager.GetInstalledPackages(packageId, true).FirstOrDefault();
if (package.AvailableVersion > package.Version)
{
Console.WriteLine("You are currently running version {currentVersion} and a newer version ({latestVersion}) is available. You should update now using Winget command 'winget {packageId}' from the Windows Terminal.", package.Version, package.AvailableVersion, packageId);
}