Skip to content

Commit b5704f7

Browse files
committed
IsInstalled property of the winget class will now check if the winget executable is still present and re-query if needed to ensure a correct result
1 parent 5d325de commit b5704f7

3 files changed

Lines changed: 58 additions & 50 deletions

File tree

‎src/WGet.NET/Components/WinGet.cs‎

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
// https://github.com/basicx-StrgV/ //
44
//--------------------------------------------------//
55
using System;
6+
using System.IO;
67
using System.Threading.Tasks;
78
using WGetNET.Models;
89
using WGetNET.Exceptions;
910
using WGetNET.HelperClasses;
10-
using System.IO;
1111

1212
namespace 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
}

‎src/WGet.NET/XmlDocumentation/WGet.NET.xml‎

Lines changed: 8 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/WGetTest/Program.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading.Tasks;
33
using System.Collections.Generic;
44
using WGetNET;
5+
using System.Diagnostics;
56

67
namespace WGetTest
78
{
@@ -19,6 +20,7 @@ static void Main(string[] args)
1920

2021
private void Run()
2122
{
23+
Stopwatch sw = Stopwatch.StartNew();
2224
try
2325
{
2426
WinGetPackageManager connector = new WinGetPackageManager();
@@ -102,6 +104,11 @@ private void Run()
102104
{
103105
Console.WriteLine(e);
104106
}
107+
finally
108+
{
109+
sw.Stop();
110+
Console.WriteLine("Execution Time: " + sw.Elapsed);
111+
}
105112
}
106113
}
107114
#pragma warning restore

0 commit comments

Comments
 (0)