33// https://github.com/basicx-StrgV/ //
44//--------------------------------------------------//
55using System ;
6+ using System . IO ;
67using System . Threading . Tasks ;
78using WGetNET . Models ;
89using WGetNET . Exceptions ;
910using WGetNET . HelperClasses ;
10- using System . IO ;
1111
1212namespace WGetNET
1313{
@@ -29,7 +29,6 @@ private protected ProcessManager ProcessManager
2929 private const string _exportSettingsCmd = "settings export" ;
3030
3131 private ProcessManager _processManager ;
32- private bool _isInstalled ;
3332 private string _wingetExePath ;
3433 private string _versionString ;
3534 private Version _version ;
@@ -44,7 +43,17 @@ public bool IsInstalled
4443 {
4544 get
4645 {
47- return _isInstalled ;
46+ // Check if the winget executable still exist to ensure a correct result,
47+ // even if winget gets removed while the application is running.
48+ if ( File . Exists ( _wingetExePath ) )
49+ {
50+ return true ;
51+ }
52+ else
53+ {
54+ // Re-query installation and return the result to allow instalation of winget while the application is running.
55+ return QueryInstallation ( ) ;
56+ }
4857 }
4958 }
5059
@@ -85,50 +94,17 @@ public WinGet()
8594
8695 if ( string . IsNullOrWhiteSpace ( _wingetExePath ) )
8796 {
88- _isInstalled = false ;
8997 _processManager = new ProcessManager ( "winget" ) ;
9098 }
9199 else
92100 {
93- _isInstalled = true ;
94101 _processManager = new ProcessManager ( _wingetExePath ) ;
95102 }
96103
97104 _versionString = CheckWinGetVersion ( ) ;
98105 _version = VersionParser . Parse ( _versionString ) ;
99106 }
100107
101- /// <summary>
102- /// Checks the system for a winget installation.
103- /// </summary>
104- /// <remarks>
105- /// <para>Can be used to check if winget got installed will the installation is running without needing to restart it.</para>
106- /// <para>This needs to be called manually, because a re-query for the installation will not be done automatically by the library.</para>
107- /// </remarks>
108- /// <returns>
109- /// <see langword="true"/> if the installation was found and <see langword="false"/> if not.
110- /// </returns>
111- public bool ReQueryInstallation ( )
112- {
113- _wingetExePath = CheckInstallation ( ) ;
114-
115- if ( string . IsNullOrWhiteSpace ( _wingetExePath ) )
116- {
117- _isInstalled = false ;
118- _processManager = new ProcessManager ( "winget" ) ;
119- }
120- else
121- {
122- _isInstalled = true ;
123- _processManager = new ProcessManager ( _wingetExePath ) ;
124- }
125-
126- _versionString = CheckWinGetVersion ( ) ;
127- _version = VersionParser . Parse ( _versionString ) ;
128-
129- return _isInstalled ;
130- }
131-
132108 /// <summary>
133109 /// Exports the WinGet settings to a json string.
134110 /// </summary>
@@ -437,7 +413,7 @@ protected bool CheckWinGetVersion(Version minVersion, Version? maxVersion = null
437413 /// </exception>
438414 protected void ThrowIfNotInstalled ( )
439415 {
440- if ( ! _isInstalled )
416+ if ( ! IsInstalled )
441417 {
442418 throw new WinGetNotInstalledException ( ) ;
443419 }
@@ -451,7 +427,7 @@ protected void ThrowIfNotInstalled()
451427 /// </returns>
452428 private string CheckWinGetVersion ( )
453429 {
454- if ( ! _isInstalled )
430+ if ( ! IsInstalled )
455431 {
456432 return string . Empty ;
457433 }
@@ -507,5 +483,34 @@ private string CheckInstallation()
507483
508484 return string . Empty ;
509485 }
486+
487+ /// <summary>
488+ /// Checks the system for a winget installation.
489+ /// </summary>
490+ /// <returns>
491+ /// <see langword="true"/> if the installation was found and <see langword="false"/> if not.
492+ /// </returns>
493+ private bool QueryInstallation ( )
494+ {
495+ bool isInstalled ;
496+
497+ _wingetExePath = CheckInstallation ( ) ;
498+
499+ if ( string . IsNullOrWhiteSpace ( _wingetExePath ) )
500+ {
501+ isInstalled = false ;
502+ _processManager = new ProcessManager ( "winget" ) ;
503+ }
504+ else
505+ {
506+ isInstalled = true ;
507+ _processManager = new ProcessManager ( _wingetExePath ) ;
508+ }
509+
510+ _versionString = CheckWinGetVersion ( ) ;
511+ _version = VersionParser . Parse ( _versionString ) ;
512+
513+ return isInstalled ;
514+ }
510515 }
511516}
0 commit comments