Reputation: 797
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:
Microsoft.Windows.SDK.Contracts
nuGet package. ReferenceUwpDesktop
nuGet Package. ReferenceI 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?
Upvotes: 1
Views: 1003
Reputation: 169340
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.
Upvotes: 1