6using System.Collections.Generic;
10using System.Threading.Tasks;
25 private ProcessManager _processManager;
26 private string _wingetExePath;
27 private DateTime _wingetExeModificationDate;
28 private string _versionString;
30 private bool _isPreview;
32 private readonly
bool _administratorPrivileges;
46 if (File.Exists(_wingetExePath))
53 return QueryInstallation();
69 if (_wingetExeModificationDate != GetLastModificationData())
90 if (_wingetExeModificationDate != GetLastModificationData())
96 return _versionString;
111 if (_wingetExeModificationDate != GetLastModificationData())
127 _administratorPrivileges = SystemHelper.CheckAdministratorPrivileges();
130 _processManager =
new ProcessManager(
"winget");
131 _wingetExePath =
string.Empty;
132 _wingetExeModificationDate = DateTime.MinValue;
133 _versionString =
string.Empty;
153 return ProcessOutputReader.ExportOutputToString(result);
173 return ProcessOutputReader.ExportOutputToString(result);
216 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file,
"file");
217 ArgsHelper.ThrowIfPathIsInvalid(file);
221 FileHelper.WriteTextToFile(file, ProcessOutputReader.ExportOutputToString(result));
267 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file,
"file");
268 ArgsHelper.ThrowIfPathIsInvalid(file);
272 await FileHelper.WriteTextToFileAsync(file, ProcessOutputReader.ExportOutputToString(result), cancellationToken);
288 List<WinGetAdminSetting> adminSettings =
new();
292 SettingsModel settings = JsonHelper.StringToObject<SettingsModel>(settingsJson);
293 if (settings ==
null)
295 return adminSettings;
298 WinGetAdminSettingBuilder builder =
new();
299 foreach (KeyValuePair<string, bool> entry
in settings.AdminSettings)
303 builder.AddEntryName(entry.Key);
304 builder.AddStatus(entry.Value);
307 if (adminSetting !=
null)
309 adminSettings.Add(adminSetting);
313 return adminSettings;
331 List<WinGetAdminSetting> adminSettings =
new();
335#if NETCOREAPP3_1_OR_GREATER
336 SettingsModel settings = await JsonHelper.StringToObjectAsync<SettingsModel>(settingsJson, cancellationToken);
338 SettingsModel settings = JsonHelper.StringToObject<SettingsModel>(settingsJson);
340 if (settings ==
null)
342 return adminSettings;
345 WinGetAdminSettingBuilder builder =
new();
346 foreach (KeyValuePair<string, bool> entry
in settings.AdminSettings)
348 if (cancellationToken.IsCancellationRequested)
350 return adminSettings;
355 builder.AddEntryName(entry.Key);
356 builder.AddStatus(entry.Value);
359 if (adminSetting !=
null)
361 adminSettings.Add(adminSetting);
365 return adminSettings;
391 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName,
"settingName");
395 return result.Success;
421 ArgsHelper.ThrowIfObjectIsNull(setting,
"setting");
453 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName,
"settingName");
457 return result.Success;
487 ArgsHelper.ThrowIfObjectIsNull(setting,
"setting");
515 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName,
"settingName");
519 return result.Success;
545 ArgsHelper.ThrowIfObjectIsNull(setting,
"setting");
577 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName,
"settingName");
581 return result.Success;
611 ArgsHelper.ThrowIfObjectIsNull(setting,
"setting");
631 InfoActionVersionId actionVersionId = InfoActionVersionId.VersionRange1;
632 if (CheckWinGetVersion(
new Version(1, 4, 3531),
new Version(1, 5, 101)))
634 actionVersionId = InfoActionVersionId.VersionRange2;
636 else if (CheckWinGetVersion(
new Version(1, 5, 441),
new Version(1, 5, 441)))
638 actionVersionId = InfoActionVersionId.VersionRange3;
640 else if (CheckWinGetVersion(
new Version(1, 5, 1081)))
642 actionVersionId = InfoActionVersionId.VersionRange4;
645 return ProcessOutputReader.ToWingetInfo(result.Output, actionVersionId);
661 public async Task<WinGetInfo>
GetInfoAsync(CancellationToken cancellationToken =
default)
666 InfoActionVersionId actionVersionId = InfoActionVersionId.VersionRange1;
667 if (CheckWinGetVersion(
new Version(1, 4, 3531),
new Version(1, 5, 101)))
669 actionVersionId = InfoActionVersionId.VersionRange2;
671 else if (CheckWinGetVersion(
new Version(1, 5, 441),
new Version(1, 5, 441)))
673 actionVersionId = InfoActionVersionId.VersionRange3;
675 else if (CheckWinGetVersion(
new Version(1, 5, 1081)))
677 actionVersionId = InfoActionVersionId.VersionRange4;
681 if (cancellationToken.IsCancellationRequested)
686 return ProcessOutputReader.ToWingetInfo(result.Output, actionVersionId);
708 ArgsHelper.ThrowIfObjectIsNull(args,
"args");
710 ThrowIfNotInstalled();
737 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(args,
"args");
739 ThrowIfNotInstalled();
768 ArgsHelper.ThrowIfObjectIsNull(args,
"args");
770 ThrowIfNotInstalled();
772 WinGetResult result =
new(await ExecuteAsync(args,
false, cancellationToken), args);
798 public async Task<WinGetResult>
ExecuteCustomAsync(
string args, CancellationToken cancellationToken =
default)
800 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(args,
"args");
802 ThrowIfNotInstalled();
806 WinGetResult result =
new(await ExecuteAsync(argsObj,
false, cancellationToken), argsObj);
825 private protected bool CheckWinGetVersion(
Version minVersion,
Version? maxVersion =
null)
828 if ((winGetVersion.Major >= minVersion.Major && winGetVersion.Minor >= minVersion.Minor &&
829 ((winGetVersion.Minor != minVersion.Minor) || (winGetVersion.Minor == minVersion.Minor && winGetVersion.Build >= minVersion.Build))) &&
830 ((maxVersion ==
null) || (winGetVersion.Major <= maxVersion.Major && winGetVersion.Minor <= maxVersion.Minor &&
831 ((winGetVersion.Minor != maxVersion.Minor) || (winGetVersion.Minor == maxVersion.Minor && winGetVersion.Build <= maxVersion.Build)))))
857 private protected ProcessResult Execute(
WinGetArguments args,
bool needsAdminRights =
false)
859 ThrowIfNotInstalled();
861 if (needsAdminRights)
866 return _processManager.ExecuteWingetProcess(args.
Arguments);
892 private protected async Task<ProcessResult> ExecuteAsync(
WinGetArguments args,
bool needsAdminRights =
false, CancellationToken cancellationToken =
default)
894 ThrowIfNotInstalled();
896 if (needsAdminRights)
901 return await _processManager.ExecuteWingetProcessAsync(args.
Arguments, cancellationToken);
913 private void ThrowIfNotInstalled()
927 private void ThrowIfNotAdmin()
929 if (!_administratorPrivileges)
931 throw new SecurityException(
"Administrator privileges are missing.");
941 private string CheckWinGetVersion()
950 for (
int i = 0; i < result.Output.Length; i++)
952#if NETCOREAPP3_1_OR_GREATER
953 if (result.Output[i].StartsWith(
'v') && result.Output[i].Length >= 2)
956 return result.Output[i].Trim()[1..];
959 if (result.Output[i].StartsWith(
"v") && result.Output[i].Length >= 2)
962 return result.Output[i].Trim().Substring(1);
976 private DateTime GetLastModificationData()
978 if (
string.IsNullOrWhiteSpace(_wingetExePath) || !File.Exists(_wingetExePath))
980 return DateTime.MinValue;
983 return File.GetLastWriteTimeUtc(_wingetExePath);
992 private bool QueryInstallation()
996 _wingetExePath = SystemHelper.CheckWingetInstallation();
998 if (
string.IsNullOrWhiteSpace(_wingetExePath))
1000 isInstalled =
false;
1001 _processManager =
new ProcessManager(
"winget");
1006 _processManager =
new ProcessManager(_wingetExePath);
1009 _wingetExeModificationDate = GetLastModificationData();
1013 _versionString = CheckWinGetVersion();
1017 _versionString =
string.Empty;
1020 _version = VersionParser.Parse(_versionString);
1022 _isPreview = VersionParser.CheckPreviewStatus(_versionString);
string EntryName
Gets the name of the info entry.
The WGetNET.WinGet class offers informations about the installed winget version.
async Task< bool > DisableAdminSettingAsync(string settingName, CancellationToken cancellationToken=default)
Asynchronously disables the provided admin setting (Needs administrator rights).
bool IsPreview
Gets if the version of winget is a preview version.
WinGet()
Initializes a new instance of the WGetNET.WinGet class.
bool IsInstalled
Gets if winget is installed on the system.
async Task< WinGetResult > ExecuteCustomAsync(WinGetArguments args, CancellationToken cancellationToken=default)
Asynchronously exectutes WinGet with the provided arguments.
async Task< List< WinGetAdminSetting > > GetAdminSettingsAsync(CancellationToken cancellationToken=default)
Asynchronously gets all winget admin settings.
Version Version
Gets the version number of the winget installation.
WinGetResult ExecuteCustom(WinGetArguments args)
Exectutes WinGet with the provided arguments.
async Task< string > ExportSettingsAsync(CancellationToken cancellationToken=default)
Asynchronous exports the WinGet settings to a json string.
async Task ExportSettingsToFileAsync(string file, CancellationToken cancellationToken=default)
Asynchronous exports the WinGet settings to a json and writes them to the given file.
List< WinGetAdminSetting > GetAdminSettings()
Gets all winget admin settings.
bool EnableAdminSetting(WinGetAdminSetting setting)
Enables the provided admin setting (Needs administrator rights).
async Task< bool > EnableAdminSettingAsync(WinGetAdminSetting setting, CancellationToken cancellationToken=default)
Asynchronously enables the provided admin setting (Needs administrator rights).
string ExportSettings()
Exports the WinGet settings to a json string.
async Task< bool > EnableAdminSettingAsync(string settingName, CancellationToken cancellationToken=default)
Asynchronously enables the provided admin setting (Needs administrator rights).
string VersionString
Gets the version number of the winget installation.
bool DisableAdminSetting(string settingName)
Disables the provided admin setting (Needs administrator rights).
WinGetInfo GetInfo()
Gets all WinGet related data provided by the WinGet info action.
bool EnableAdminSetting(string settingName)
Enables the provided admin setting (Needs administrator rights).
async Task< WinGetInfo > GetInfoAsync(CancellationToken cancellationToken=default)
Asynchronous gets all WinGet related data provided by the WinGet info action.
bool DisableAdminSetting(WinGetAdminSetting setting)
Disables the provided admin setting (Needs administrator rights).
void ExportSettingsToFile(string file)
Exports the WinGet settings to a json and writes them to the given file.
async Task< bool > DisableAdminSettingAsync(WinGetAdminSetting setting, CancellationToken cancellationToken=default)
Asynchronously disables the provided admin setting (Needs administrator rights).
WinGetResult ExecuteCustom(string args)
Exectutes WinGet with the provided arguments.
async Task< WinGetResult > ExecuteCustomAsync(string args, CancellationToken cancellationToken=default)
Asynchronously exectutes WinGet with the provided arguments.
Represents a winget admin settings entry.
Represents a winget arguments string for different winget actions.
static WinGetArguments WinGet()
Creates a new winget arguments object with no base cmd. Used for direct callin of the winget cmd with...
WinGetArguments Disable(string query)
Adds a disable action to the arguments.
static WinGetArguments SettingsExport()
Creates a new winget arguments object with "settings export" as the base cmd.
string Arguments
Gets the generated arguments.
static WinGetArguments Settings()
Creates a new winget arguments object with "settings" as the base cmd.
static WinGetArguments CustomCmd(string cmd)
Creates a new winget arguments object with a custom base cmd.
WinGetArguments Info()
Adds the '–info' flag to the arguments.
WinGetArguments Enable(string query)
Adds a enable action to the arguments.
WinGetArguments Version(string version)
Adds a version query to the arguments.
Represents winget related information.
Represents a winget execution result.
Exception that gets thrown if winget is not installed.