Skip to content

Commit c5a0d7d

Browse files
committed
Added the function to request silent package uninstall
1 parent 041d9c4 commit c5a0d7d

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

src/WGet.NET/Components/WinGetPackageManager.cs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,39 @@ public bool UninstallPackage(string packageId)
862862
return result.Success;
863863
}
864864

865+
/// <summary>
866+
/// Uninsatll a package using winget.
867+
/// </summary>
868+
/// <param name="packageId">The id or name of the package for uninstallation.</param>
869+
/// <param name="silent">Request silent package uninstall.</param>
870+
/// <returns>
871+
/// <see langword="true"/> if the uninstallation was successful or <see langword="false"/> if it failed.
872+
/// </returns>
873+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
874+
/// WinGet is not installed or not found on the system.
875+
/// </exception>
876+
/// <exception cref="System.ArgumentException">
877+
/// A provided argument is empty.
878+
/// </exception>
879+
/// <exception cref="System.ArgumentNullException">
880+
/// A provided argument is null.
881+
/// </exception>
882+
public bool UninstallPackage(string packageId, bool silent)
883+
{
884+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");
885+
886+
string cmd = string.Format(_uninstallCmd, packageId);
887+
888+
if (silent)
889+
{
890+
cmd = Silent(cmd);
891+
}
892+
893+
ProcessResult result = Execute(cmd);
894+
895+
return result.Success;
896+
}
897+
865898
/// <summary>
866899
/// Uninstall a package using winget.
867900
/// </summary>
@@ -890,6 +923,35 @@ public bool UninstallPackage(WinGetPackage package)
890923
return UninstallPackage(package.Id);
891924
}
892925

926+
/// <summary>
927+
/// Uninstall a package using winget.
928+
/// </summary>
929+
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> for the uninstallation.</param>
930+
/// <param name="silent" > Request silent package uninstall.</param>
931+
/// <returns>
932+
/// <see langword="true"/> if the uninstallation was successful or <see langword="false"/> if it failed.
933+
/// </returns>
934+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
935+
/// WinGet is not installed or not found on the system.
936+
/// </exception>
937+
/// <exception cref="System.ArgumentException">
938+
/// A provided argument is empty.
939+
/// </exception>
940+
/// <exception cref="System.ArgumentNullException">
941+
/// A provided argument is null.
942+
/// </exception>
943+
public bool UninstallPackage(WinGetPackage package, bool silent)
944+
{
945+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
946+
947+
if (package.HasShortenedId || package.HasNoId)
948+
{
949+
return UninstallPackage(package.Name, silent);
950+
}
951+
952+
return UninstallPackage(package.Id, silent);
953+
}
954+
893955
/// <summary>
894956
/// Asynchronously uninsatll a package using winget.
895957
/// </summary>
@@ -922,6 +984,44 @@ public async Task<bool> UninstallPackageAsync(string packageId, CancellationToke
922984
return result.Success;
923985
}
924986

987+
/// <summary>
988+
/// Asynchronously uninsatll a package using winget.
989+
/// </summary>
990+
/// <param name="packageId">The id or name of the package for uninstallation.</param>
991+
/// <param name="silent" > Request silent package uninstall.</param>
992+
/// <param name="cancellationToken">
993+
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
994+
/// </param>
995+
/// <returns>
996+
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
997+
/// The result is <see langword="true"/> if the uninstallation was successful or <see langword="false"/> if it failed.
998+
/// </returns>
999+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
1000+
/// WinGet is not installed or not found on the system.
1001+
/// </exception>
1002+
/// <exception cref="System.ArgumentException">
1003+
/// A provided argument is empty.
1004+
/// </exception>
1005+
/// <exception cref="System.ArgumentNullException">
1006+
/// A provided argument is null.
1007+
/// </exception>
1008+
public async Task<bool> UninstallPackageAsync(string packageId, bool silent, CancellationToken cancellationToken = default)
1009+
{
1010+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");
1011+
1012+
string cmd = string.Format(_uninstallCmd, packageId);
1013+
1014+
if (silent)
1015+
{
1016+
cmd = Silent(cmd);
1017+
}
1018+
1019+
ProcessResult result =
1020+
await ExecuteAsync(cmd, false, cancellationToken);
1021+
1022+
return result.Success;
1023+
}
1024+
9251025
/// <summary>
9261026
/// Asynchronously uninstall a package using winget.
9271027
/// </summary>
@@ -953,6 +1053,39 @@ public async Task<bool> UninstallPackageAsync(WinGetPackage package, Cancellatio
9531053

9541054
return await UninstallPackageAsync(package.Id, cancellationToken);
9551055
}
1056+
1057+
/// <summary>
1058+
/// Asynchronously uninstall a package using winget.
1059+
/// </summary>
1060+
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> for the uninstallation.</param>
1061+
/// <param name="silent" > Request silent package uninstall.</param>
1062+
/// <param name="cancellationToken">
1063+
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
1064+
/// </param>
1065+
/// <returns>
1066+
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
1067+
/// The result is <see langword="true"/> if the uninstallation was successful or <see langword="false"/> if it failed.
1068+
/// </returns>
1069+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
1070+
/// WinGet is not installed or not found on the system.
1071+
/// </exception>
1072+
/// <exception cref="System.ArgumentException">
1073+
/// A provided argument is empty.
1074+
/// </exception>
1075+
/// <exception cref="System.ArgumentNullException">
1076+
/// A provided argument is null.
1077+
/// </exception>
1078+
public async Task<bool> UninstallPackageAsync(WinGetPackage package, bool silent, CancellationToken cancellationToken = default)
1079+
{
1080+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
1081+
1082+
if (package.HasShortenedId || package.HasNoId)
1083+
{
1084+
return await UninstallPackageAsync(package.Name, silent, cancellationToken);
1085+
}
1086+
1087+
return await UninstallPackageAsync(package.Id, silent, cancellationToken);
1088+
}
9561089
//---------------------------------------------------------------------------------------------
9571090

9581091
//---List Upgrades-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)