I have created the UWP application. I have used some API which is available only in the latest version(19041, 17763) of UWP. I want to run in both pervious and latest version. So if I get the apps target version programmatically, I will check the condition which makes the app compatible for every versions. Is it possible to get the apps target version.
2 Answers
you can use powershell command to get version from file metadata (you must make sure your product have a valid value there)
and then you can use the following logic: on path argument send path to app executable file (or dll) with file metadata updated field.
string GetVersion(string path)
{
var command = $"(Get-Command \"{path}\").FileVersionInfo.ProductVersion";
var processStartInfo = new ProcessStartInfo
{
FileName = "cli",
Arguments = $"/c powershell {command}",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
UseShellExecute = false
};
var p = System.Diagnostics.Process.Start(proc);
p.WaitForExit();
var fileVersion = p.StandardOutput.ReadToEnd();
return fileVersion;
}
This solution works well on local and remote targets..
if you need it locally only you can simply use FileVersionInfo class and get it out of the FileVersion property
as given in MSDN example:
public static void Main(string[] args)
{
// Get the file version for the notepad.
// Use either of the two following commands.
FileVersionInfo.GetVersionInfo(Path.Combine(Environment.SystemDirectory, "Notepad.exe"));
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(Environment.SystemDirectory + "\\Notepad.exe");
// Print the file name and version number.
Console.WriteLine("File: " + myFileVersionInfo.FileDescription + '\n' +
"Version number: " + myFileVersionInfo.FileVersion);
}
you can read more here: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.fileversioninfo?view=net-5.0

- 413
- 1
- 4
- 20
-
Thanks for the code, Is this is the only way to get the target build version. – sam Apr 11 '21 at 07:57
-
Edited and added another approach. – Michael Gabbay Apr 11 '21 at 08:19
-
Hi @michael Gabbay, I have heard some Api's to get the target version like AnalyticsVersionInfo. I have tried out this but it doesn't give correct version. Is there any api like that to get the target version. – sam Apr 11 '21 at 12:15
-
try see this question https://stackoverflow.com/questions/28635208/retrieve-the-current-app-version-from-package – Michael Gabbay Apr 11 '21 at 16:17
-
I need to get the targetsdkversion in which my uwpapp is running. For example, Im set my target version as **Windows 10, Version 1809(10.0 Build 17763)**, I need this target version programmatically. – sam Apr 12 '21 at 03:55
You could get the AppxManifest.xml file in the app installation folder. After that, you could load the xml file using Windows.Data.Xml.Dom.XmlDocument Class. In the AppxManifest.xml, you could find a node called TargetDeviceFamily under Package->Dependencies, it contains a value called MaxVersionTested which is the value of the target version.
Here is the code snippet that you could check:
var file = await Package.Current.InstalledLocation.GetFileAsync("AppxManifest.xml");
var xmlDocument = await XmlDocument.LoadFromFileAsync(file);
var VersionText = xmlDocument.ChildNodes.LastOrDefault().ChildNodes.Where(p => p.NodeName == "Dependencies").FirstOrDefault().ChildNodes.Where(t => t.NodeName == "TargetDeviceFamily").FirstOrDefault().Attributes.GetNamedItem("MaxVersionTested").InnerText;
MyTextBlock.Text = VersionText;

- 8,043
- 1
- 7
- 13