Reputation: 31
I'm able to build, deploy, and install my project using .NET Core 3.0 WPF and sideloading (Windows Store app packages but from a local UNC path). This all seems to be working fine but I can't find a way to get the current version number through code to display to the user while the app is running.
I've tried the way I've always done in ClickOnce using reflection and XML. I've also tried to use the UWP method I've seen posted elsewhere and can't seem to use Windows.ApplicationModel in WPF/.NET Core 3
This way worked for me in .NET Framework with ClickOnce
XmlDocument xmlDoc = new System.Xml.XmlDocument();
Assembly asmCurrent = System.Reflection.Assembly.GetExecutingAssembly();
string executePath = new Uri(asmCurrent.GetName().CodeBase).LocalPath;
xmlDoc.Load(executePath + ".manifest");
string retval = string.Empty;
if (xmlDoc.HasChildNodes)
{
retval = xmlDoc.ChildNodes[1].ChildNodes[0].Attributes.GetNamedItem("version").Value.ToString();
}
return new Version(retval).ToString();
This looks like what people are doing with UWP and Windows Store
Package package = Windows.ApplicationModel.Package.Current;
PackageId packageId = package.Id;
PackageVersion version = packageId.Version;
I've also tried doing this with Reflection but it always return 1.0.0.0, even when deployed.
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
return fvi.FileVersion;
Upvotes: 2
Views: 1340
Reputation: 469
You can simply load the MSIX's manifest:
private Version GetMsixPackageVersion()
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var manifestPath = assembly.Location.Replace(assembly.ManifestModule.Name, "") + "\\AppxManifest.xml";
if (File.Exists(manifestPath))
{
var xDoc = XDocument.Load(manifestPath);
return new Version(xDoc.Descendants().First(e => e.Name.LocalName == "Identity").Attributes()
.First(a => a.Name.LocalName == "Version").Value);
}
return new Version(0,0,0,0);
}
Note that it's not available during development, although I simply copied a manifest to the debug folder to do my testing.
Upvotes: 1