sam
sam

Reputation: 9

How to get the UWP(apps) target appversion programmatically?

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.

Upvotes: 0

Views: 558

Answers (2)

Roy Li - MSFT
Roy Li - MSFT

Reputation: 8681

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;

Upvotes: 1

Michael Gabbay
Michael Gabbay

Reputation: 465

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

Upvotes: 1

Related Questions