Skip to content

Commit

Permalink
Improve Windows version detection for API instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
widavies committed Sep 7, 2023
1 parent 7196b21 commit f0e2281
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
15 changes: 7 additions & 8 deletions WinJump/Core/VirtualDesktopDefinitions/IVirtualDesktopAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace WinJump.Core.VirtualDesktopDefinitions;
/// for these functions.
/// </summary>
public interface IVirtualDesktopAPI : IDisposable {

/// <summary>
/// An event that notifies subscribers when the virtual desktop changes.
/// </summary>
Expand All @@ -33,18 +32,18 @@ public interface IVirtualDesktopAPI : IDisposable {
/// <returns>A virtual desktop API for the installed Windows version</returns>
/// <exception cref="Exception">If the particular Windows version is unsupported</exception>
public static IVirtualDesktopAPI Create() {
string? releaseId = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion",
"CurrentBuildNumber", "")?.ToString();
string? releaseId = Registry.LocalMachine.OpenSubKey("SOFTWARE")?.OpenSubKey("Microsoft")?
.OpenSubKey("Windows NT")?.OpenSubKey("CurrentVersion")?.GetValue("CurrentBuildNumber")?.ToString();

string? releaseBuild = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion",
"UBR", "")?.ToString();
string? releaseBuild = Registry.LocalMachine.OpenSubKey("SOFTWARE")?.OpenSubKey("Microsoft")?
.OpenSubKey("Windows NT")?.OpenSubKey("CurrentVersion")?.GetValue("UBR")?.ToString();

if(!int.TryParse(releaseId, out int releaseIdNumber)) {
throw new Exception("Unrecognized Windows release id version");
throw new Exception($"Unrecognized Windows release id version {releaseId}.{releaseBuild}");
}

if(!int.TryParse(releaseBuild, out int releaseBuildNumber)) {
throw new Exception("Unrecognized Windows build version");
throw new Exception($"Unrecognized Windows build version {releaseId}.{releaseBuild}");
}

return releaseIdNumber switch {
Expand All @@ -54,7 +53,7 @@ public static IVirtualDesktopAPI Create() {
: new Windows11_22621.VirtualDesktopApi(),
>= 22000 => new Windows11_22000.VirtualDesktopApi(),
>= 17763 => new Windows10_17763.VirtualDesktopApi(),
_ => throw new Exception($"Unsupported Windows version {releaseId}.{releaseBuildNumber}")
_ => throw new Exception($"Unsupported Windows version {releaseIdNumber}.{releaseBuildNumber}")
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions WinJump/WinJump.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<Version>2.0.4</Version>
<AssemblyVersion>2.0.5</AssemblyVersion>
<FileVersion>2.0.5</FileVersion>
<AssemblyVersion>2.0.6</AssemblyVersion>
<FileVersion>2.0.6</FileVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
Expand Down

0 comments on commit f0e2281

Please sign in to comment.