@@ -229,7 +229,7 @@ public async Task<List<WinGetPackage>> SearchPackageAsync(string packageId, stri
229229 }
230230 //---------------------------------------------------------------------------------------------
231231
232- //---Install -----------------------------------------------------------------------------------
232+ //---List--- -----------------------------------------------------------------------------------
233233 /// <summary>
234234 /// Gets a list of all installed packages.
235235 /// </summary>
@@ -440,6 +440,148 @@ public async Task<List<WinGetPackage>> GetInstalledPackagesAsync(string packageI
440440 return ProcessOutputReader . ToPackageList ( result . Output , PackageAction . InstalledListBySource , sourceName ) ;
441441 }
442442
443+ /// <summary>
444+ /// Gets a installed package, that matchs the provided id/name. If there are multiple matches, the first match will be returned.
445+ /// </summary>
446+ /// <remarks>
447+ /// This method does an internal match and does not use the winget "exact" functionality.
448+ /// </remarks>
449+ /// <param name="packageId">
450+ /// The id or name of the package for the search.
451+ /// </param>
452+ /// <returns>
453+ /// A <see cref="WGetNET.WinGetPackage"/> instances or <see langword="null"/> if no match was found.
454+ /// </returns>
455+ /// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
456+ /// WinGet is not installed or not found on the system.
457+ /// </exception>
458+ /// <exception cref="System.ArgumentException">
459+ /// A provided argument is empty.
460+ /// </exception>
461+ /// <exception cref="System.ArgumentNullException">
462+ /// A provided argument is null.
463+ /// </exception>
464+ public WinGetPackage ? GetExactInstalledPackage ( string packageId )
465+ {
466+ ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( packageId , "packageId" ) ;
467+
468+ ProcessResult result = Execute ( string . Format ( AcceptSourceAgreements ( _searchInstalledCmd ) , packageId ) ) ;
469+
470+ return MatchExact (
471+ ProcessOutputReader . ToPackageList ( result . Output , PackageAction . InstalledList ) ,
472+ packageId . Trim ( )
473+ ) ;
474+ }
475+
476+ /// <summary>
477+ /// Gets a installed package, that matchs the provided id/name. If there are multiple matches, the first match will be returned.
478+ /// </summary>
479+ /// <remarks>
480+ /// This method does an internal match and does not use the winget "exact" functionality.
481+ /// </remarks>
482+ /// <param name="packageId">
483+ /// The id or name of the package for the search.
484+ /// </param>
485+ /// <param name="sourceName">
486+ /// The name of the source for the search.
487+ /// </param>
488+ /// <returns>
489+ /// A <see cref="WGetNET.WinGetPackage"/> instances or <see langword="null"/> if no match was found.
490+ /// </returns>
491+ /// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
492+ /// WinGet is not installed or not found on the system.
493+ /// </exception>
494+ /// <exception cref="System.ArgumentException">
495+ /// A provided argument is empty.
496+ /// </exception>
497+ /// <exception cref="System.ArgumentNullException">
498+ /// A provided argument is null.
499+ /// </exception>
500+ public WinGetPackage ? GetExactInstalledPackage ( string packageId , string sourceName )
501+ {
502+ ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( packageId , "packageId" ) ;
503+ ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( sourceName , "sourceName" ) ;
504+
505+ ProcessResult result = Execute ( string . Format ( AcceptSourceAgreements ( _searchInstalledBySourceCmd ) , packageId , sourceName ) ) ;
506+
507+ return MatchExact (
508+ ProcessOutputReader . ToPackageList ( result . Output , PackageAction . InstalledList ) ,
509+ packageId . Trim ( )
510+ ) ;
511+ }
512+
513+ /// <summary>
514+ /// Asynchronously gets a installed package, that matchs the provided id/name. If there are multiple matches, the first match will be returned.
515+ /// </summary>
516+ /// <remarks>
517+ /// This method does an internal match and does not use the winget "exact" functionality.
518+ /// </remarks>
519+ /// <param name="packageId">
520+ /// The id or name of the package for the search.
521+ /// </param>
522+ /// <returns>
523+ /// A <see cref="WGetNET.WinGetPackage"/> instances or <see langword="null"/> if no match was found.
524+ /// </returns>
525+ /// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
526+ /// WinGet is not installed or not found on the system.
527+ /// </exception>
528+ /// <exception cref="System.ArgumentException">
529+ /// A provided argument is empty.
530+ /// </exception>
531+ /// <exception cref="System.ArgumentNullException">
532+ /// A provided argument is null.
533+ /// </exception>
534+ public async Task < WinGetPackage ? > GetExactInstalledPackageAsync ( string packageId )
535+ {
536+ ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( packageId , "packageId" ) ;
537+
538+ ProcessResult result = await ExecuteAsync ( string . Format ( AcceptSourceAgreements ( _searchInstalledCmd ) , packageId ) ) ;
539+
540+ return MatchExact (
541+ ProcessOutputReader . ToPackageList ( result . Output , PackageAction . InstalledList ) ,
542+ packageId . Trim ( )
543+ ) ;
544+ }
545+
546+ /// <summary>
547+ /// Asynchronously gets a installed package, that matchs the provided id/name. If there are multiple matches, the first match will be returned.
548+ /// </summary>
549+ /// <remarks>
550+ /// This method does an internal match and does not use the winget "exact" functionality.
551+ /// </remarks>
552+ /// <param name="packageId">
553+ /// The id or name of the package for the search.
554+ /// </param>
555+ /// <param name="sourceName">
556+ /// The name of the source for the search.
557+ /// </param>
558+ /// <returns>
559+ /// A <see cref="WGetNET.WinGetPackage"/> instances or <see langword="null"/> if no match was found.
560+ /// </returns>
561+ /// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
562+ /// WinGet is not installed or not found on the system.
563+ /// </exception>
564+ /// <exception cref="System.ArgumentException">
565+ /// A provided argument is empty.
566+ /// </exception>
567+ /// <exception cref="System.ArgumentNullException">
568+ /// A provided argument is null.
569+ /// </exception>
570+ public async Task < WinGetPackage ? > GetExactInstalledPackageAsync ( string packageId , string sourceName )
571+ {
572+ ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( packageId , "packageId" ) ;
573+ ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( sourceName , "sourceName" ) ;
574+
575+ ProcessResult result = await ExecuteAsync ( string . Format ( AcceptSourceAgreements ( _searchInstalledBySourceCmd ) , packageId , sourceName ) ) ;
576+
577+ return MatchExact (
578+ ProcessOutputReader . ToPackageList ( result . Output , PackageAction . InstalledList ) ,
579+ packageId . Trim ( )
580+ ) ;
581+ }
582+ //---------------------------------------------------------------------------------------------
583+
584+ //---Install-----------------------------------------------------------------------------------
443585 /// <summary>
444586 /// Install a package using winget.
445587 /// </summary>
@@ -2685,6 +2827,36 @@ private string AcceptSourceAgreements(string argument)
26852827
26862828 return argument ;
26872829 }
2830+
2831+ /// <summary>
2832+ /// Tries to match a <see cref="WGetNET.WinGetPackage"/> to the provided search criteria.
2833+ /// </summary>
2834+ /// <param name="matchList">
2835+ /// The list to try and finde match in.
2836+ /// </param>
2837+ /// <param name="matchString">
2838+ /// The search criteria, that will be tried to be matched againts the package id and/or name.
2839+ /// </param>
2840+ /// <returns>
2841+ /// The <see cref="WGetNET.WinGetPackage"/> that matches the search criteria, or <see langword="null"/> if no package matches.
2842+ /// </returns>
2843+ private WinGetPackage ? MatchExact ( List < WinGetPackage > matchList , string matchString )
2844+ {
2845+ if ( matchList == null || matchList . Count <= 0 || string . IsNullOrWhiteSpace ( matchString ) )
2846+ {
2847+ return null ;
2848+ }
2849+
2850+ for ( int i = 0 ; i < matchList . Count ; i ++ )
2851+ {
2852+ if ( string . Equals ( matchList [ i ] . Id , matchString ) || string . Equals ( matchList [ i ] . Name , matchString ) )
2853+ {
2854+ return matchList [ i ] ;
2855+ }
2856+ }
2857+
2858+ return null ;
2859+ }
26882860 //---------------------------------------------------------------------------------------------
26892861 }
26902862}
0 commit comments