locked
How to get the package version in Windows Phone 8? RRS feed

  • Question

  • I have been trying to get the package version number in my Windows Phone 8 application.

    If I use the following code to get the PackageId I get a System.NotImplementedException:

    var package = Windows.ApplicationModel.Package.Current;
    PackageId packageId = package.Id;
    The documentation here states that this should work on Windows Phone 8:
    http://msdn.microsoft.com/en-us/library/windowsphone/develop/windows.applicationmodel.package.id.aspx

    Is the documentation wrong and there is a different way to get the version on the phone?


    Tuesday, February 26, 2013 8:38 PM

All replies

  • I'm looking into this and will get back to you soon.
    Tuesday, February 26, 2013 10:03 PM
  • I too would like to know if this is possible. Was there ever a resolution to this?
    Thursday, March 21, 2013 3:00 PM
  • I've figured out how to get it directly from the manifest. Future updates could end up breaking this, beware. (i.e. encryption of the manifest) Also, it could be improved...i.e. real XML parsing, and I wasn't sure which encoding it used so I just manually did an ASCII conversion ba->string.

    I'd recommend sticking it in a try block.

    				StorageFile manifest = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("WMAppManifest.xml");
    				IRandomAccessStream stream = await manifest.OpenReadAsync();
    				DataReader dr = new DataReader(stream.GetInputStreamAt(0));
    				byte[] ba = new byte[stream.Size];
    				await dr.LoadAsync((uint)ba.Length);
    				dr.ReadBytes(ba);
    				char[] ca = new char[ba.Length];
    				for (int i = 0; i < ba.Length; i++)
    					ca[i] = (char)ba[i];
    				string manifestStr = new string(ca);
    				const string versionSearch = " Version=\"";
    				int versionPos = manifestStr.IndexOf(versionSearch);
    				if (versionPos < 0)
    					log("Cannot determine version.");
    				else
    				{
    					versionPos += versionSearch.Length;
    					string versionString = manifestStr.Substring(versionPos, manifestStr.IndexOf('\"', versionPos) - versionPos);
    					string[] versionBits = versionString.Split('.');
    					if (versionBits.Length > 0) m_versionMajor = uint.Parse(versionBits[0]);
    					if (versionBits.Length > 1) m_versionMinor = uint.Parse(versionBits[1]);
    					if (versionBits.Length > 2) m_versionMicro = uint.Parse(versionBits[2]);
    					if (versionBits.Length > 3) m_versionNano = uint.Parse(versionBits[3]);
    				}
    Thursday, April 25, 2013 10:28 PM
  • Maybe change the 'var' type into 'Package '

    Here is my code. It runs well on my wp8.1 project.

     public static string GetAppVersion()
            {
                Package package = Windows.ApplicationModel.Package.Current;
                PackageId packageId = package.Id;
                PackageVersion version = packageId.Version;
                string temp= String.Format("{0}.{0}.{0}.{0}",version.Major, version.Minor, version.Build, version.Revision);
                return temp;
            }

    Monday, March 30, 2015 10:23 AM
  • Please try this.

    public void GetVersionNumber()
            {
                var asm = Assembly.GetExecutingAssembly();
                var parts = asm.FullName.Split(',');

                string versioninfo = parts[1].Split('=')[1];
            } 

    This method will provide you version information.

    • Proposed as answer by Imtiyazk Monday, March 30, 2015 10:50 AM
    Monday, March 30, 2015 10:41 AM
  • I think you meantString.Format("{0}.{1}.{2}.{3}", etc.
    Tuesday, April 21, 2015 9:47 AM
  • Please try this.

    public void GetVersionNumber()
            {
                var asm = Assembly.GetExecutingAssembly();
                var parts = asm.FullName.Split(',');

                string versioninfo = parts[1].Split('=')[1];
            } 

    This method will provide you version information.

    • Proposed as answer by Imtiyazk Tuesday, April 21, 2015 12:23 PM
    Tuesday, April 21, 2015 12:23 PM
  • This gets the assembly version, not the package version.

    Darin R.

    Tuesday, April 21, 2015 4:55 PM
  • Maybe change the 'var' type into 'Package '

    Here is my code. It runs well on my wp8.1 project.

     public static string GetAppVersion()
            {
                Package package = Windows.ApplicationModel.Package.Current;
                PackageId packageId = package.Id;
                PackageVersion version = packageId.Version;
                string temp= String.Format("{0}.{0}.{0}.{0}",version.Major, version.Minor, version.Build, version.Revision);
                return temp;
            }

    string temp= String.Format("{0}.{0}.{0}.{0}",version.Major, version.Minor, version.Build, version.Revision);
    should be

    string temp= String.Format("{0}.{1}.{2}.{3}",version.Major, version.Minor, version.Build, version.Revision);


    Satya R Biswal

    Friday, February 5, 2016 4:19 AM