Fix os check warning.

This commit is contained in:
Eugene Wang
2026-01-01 23:30:39 -05:00
parent 0c8f0fc27d
commit 951f47e833
2 changed files with 35 additions and 3 deletions

View File

@@ -8,9 +8,40 @@ namespace NTwain;
/// </summary>
static class OperatingSystem
{
public static bool IsWindows()
static readonly bool _isWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;
public static bool IsWindows() => _isWindows;
// copied from .net logic
public static bool IsWindowsVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0)
=> IsWindows() && IsOSVersionAtLeast(major, minor, build, revision);
private static bool IsOSVersionAtLeast(int major, int minor, int build, int revision)
{
return Environment.OSVersion.Platform == PlatformID.Win32NT;
Version current = Environment.OSVersion.Version;
if (current.Major != major)
{
return current.Major > major;
}
if (current.Minor != minor)
{
return current.Minor > minor;
}
// Unspecified build component is to be treated as zero
int currentBuild = current.Build < 0 ? 0 : current.Build;
build = build < 0 ? 0 : build;
if (currentBuild != build)
{
return currentBuild > build;
}
// Unspecified revision component is to be treated as zero
int currentRevision = current.Revision < 0 ? 0 : current.Revision;
revision = revision < 0 ? 0 : revision;
return currentRevision >= revision;
}
}
#endif

View File

@@ -334,7 +334,8 @@ namespace NTwain
{
try
{
_ = CloseDSMAsync();
if (OperatingSystem.IsWindowsVersionAtLeast(5, 1, 2600))
_ = CloseDSMAsync();
}
catch (InvalidOperationException) { }
}