1

I have an internal WPF application (.NET Framework 4.8), that has recently been changed to utilize the Windows Application Packaging Project. Using side loading and auto updating (on run). This is all setup and working fine; certificates are installed, app deploys properly, and it updates automatically.

What I would like to do now, is update our current display of the version number. Originally we were deploying via ClickOnce and utilized ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString() to get the Version number.

I have been struggling for a while as to how to accomplish this with the new deployment strategy.

As far as I have found I need to use Windows.ApplicationModel in order to use the Package class. But for the life of me I cannot figure out how to bring this package into my WPF app (The MainWindow.xaml.cs to be exact). I need the same code as Here.

What I have currently tried:

  • Importing the Microsoft.Windows.SDK.Contracts nuGet package. Reference
  • Importing the UwpDesktop nuGet Package. Reference
  • Following the blog Here
  • Review Microsoft documentation Here and Here
  • and other internet searches.

I have been able to get to the point where I can have a using Windows.ApplicationModel in the application without error but I still cant access the Package class.

The only things I need to accomplish is to get the version number to display in the footer bar, and to know if there is a new version so we can prompt the user to restart the application. We have no need for any of the other UWP/WinRT libraries. We have some elements to the software that have expensive affect on the real world, and do not want a silent update. People will typically open the software and not close it for the entire day. Most users could then run the software without knowing there is a fix or improvement.

My last resort would be to manually query and parse the deployment location, checking for a new version, but I am still not sure how to get the local application version.

Most of the resources I have been reading are more than 2 years old and it seems a lot has changed with the interaction for Desktop Bridges and other deployment related elements with UWP. I get the feeling that something has changed just enough to cause all this headache, or I am completely missing something stupid and it is all me. I also have zero experience with UWP so some of the documentation is a little cryptic.

We needed to change to this deployment strategy for fairly important reasons relating to the certificates and some libraries we use. It would be more headache to maintain the deployment with ClickOnce, than the current headache trying to solve this.

Does anyone know of a way to retrieve the version number of a WPF application deployed via a Windows Application Packaging Project?

Ginger Ninja
  • 759
  • 8
  • 21
  • It `ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString()` does not work? what happened when using this? – Ax1le May 12 '22 at 06:15
  • That is a library for ClickOnce. So now it returns null. – Ginger Ninja May 12 '22 at 14:59
  • Can you get the version from WPF application using `Assembly.GetExecutingAssembly().GetName().Version.ToString()`? – Roy Li - MSFT May 17 '22 at 06:33
  • @RoyLi-MSFT That seems to return the version from the `AssemblyInfo.cs`. While I am looking for the version of the package the application is deployed with. – Ginger Ninja May 18 '22 at 15:37
  • Can you keep this value the same as the version of the package every time when you change the package version? So when you get this value, you get the package version you want. – Roy Li - MSFT May 19 '22 at 03:10

1 Answers1

1

If your WPF application targets an earlier version than .NET 5, including all versions of the .NET Framework, you should install the Microsoft.Windows.SDK.Contracts NuGet package into the WPF application project.

You can then use the Windows.ApplicationModel.Package property assuming you run the packaging project (and not the unpackaged WPF app directly):

var version = Windows.ApplicationModel.Package.Current.Id.Version;

Also make sure that the target version of the packaging project is Windows 10, version 1803 or later.

If you target .NET 5 or later, you should not install the Microsoft.Windows.SDK.Contracts package. Instead, you should change the value of the <TargetFramework> element in the .csproj project file of the WPF application project to a Windows OS version-specific TFM:

<TargetFramework>net5.0-windows10.0.17763.0</TargetFramework>

You can then use the Package property just like before.

Please refer to the docs for more information about how to call WinRT APIs in desktop apps.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks for the response. I must be missing something. We are using .NET Framework 4.8 for the application. I tried installing that package and I still get a compilation error on `Windows.ApplicationModel` not existing in the `Windows` namespace. As an experiment to make sure it wasnt our app, I created a blank WPF .NET Framework 4.8 app (with the package), and I get the same compilation error. Could it be possible I need to install something? Although we do have other projects with .NET Core so I would assume I have any required SDK's installed. – Ginger Ninja May 12 '22 at 15:51
  • I did notice in the docs you linked to that it "only works in projects that target Windows 10, version 1803". Does this mean I have to have the WPF Project target said version? The packager does target the correct version, but I do not know of a way to have a .NET Framework WPF Project version target a specific version of windows, like the one you put for .NET5.0 – Ginger Ninja May 12 '22 at 16:08
  • The target version of the `Windows Application Packaging Project` must be Windows 10, version 1803 or later. Did you read the docs I linked to? – mm8 May 13 '22 at 12:55
  • Yes, I have it targeting Windows 10 1803 as the minimum version, and version 2104 as the target version. – Ginger Ninja May 13 '22 at 15:02