7

We have a .NET Core application which only will be used in-house. We changed from Click-Once to MSIX during our switch from WPF to .NET Core. In the window caption/title of our application we also "output" the current version (major, minor, ...). Previously, we took the version of our startup project (called "view"). Now using MSIX, this project has got the version number we need (the startup project is referenced to "view"). How can we read the correct version now?

Using Assembly.GetEntryAssembly or Assembly.GetCallingAssembly returns the wrong version - the version of the startup project. The application is not in the Windows Store, it will be side loaded as a package. Any ideas to get the "correct" version we "produce" when deploying our package?

jps
  • 20,041
  • 15
  • 75
  • 79
dsTny
  • 150
  • 1
  • 10

2 Answers2

3

You need to install the Windows 10 WinRT API pack. Install from Nuget the package: Microsoft.Windows.SDK.Contracts

URL: https://www.nuget.org/packages/Microsoft.Windows.SDK.Contracts

Then you can do something like this:

var version = Windows.ApplicationModel.Package.Current.Id.Version;
applicationVersion = string.Format("{0}.{1}.{2}.{3}",
    version.Major,
    version.Minor,
    version.Build,
    version.Revision);

If you want to DEBUG or Run with the current Package available, just set your package deployment project as the Startup Project.

Additional References:

https://blogs.windows.com/windowsdeveloper/2019/09/30/windows-10-winrt-api-packs-released/

https://learn.microsoft.com/en-us/uwp/api/

https://www.thomasclaudiushuber.com/2019/04/26/calling-windows-10-apis-from-your-wpf-application/

Doug Knudsen
  • 935
  • 9
  • 15
  • Sorry for my late responding. Thanks, this solution works like a charm, so I accepted this answer :) – dsTny Oct 09 '20 at 08:37
  • 1
    For .Net 5 there's no need to install anything, just change in csproj TargetFramework from (for example) net5.0-windows to net5.0-windows10.0.17763 or net5.0-windows10.0.19041.1 or whatever version you need. – Lev Jun 11 '21 at 12:24
  • Note: This works in .NET Core but not in .NET 5. – Rye bread Jul 02 '21 at 12:42
0

Call Windows Runtime APIs in desktop apps

For .NET 6 and later:

Modify TargetFramework in project file.

<TargetFramework>net6.0</TargetFramework>

To

<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>

Or any of the below versions.

net6.0-windows10.0.17763.0: If your app targets Windows 10, version 1809.

net6.0-windows10.0.18362.0: If your app targets Windows 10, version 1903.

net6.0-windows10.0.19041.0: If your app targets Windows 10, version 2004.

net6.0-windows10.0.22000.0: If your app targets Windows 11.

https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/desktop-to-uwp-enhance

Owen Lee
  • 349
  • 1
  • 5
  • 19