How to Get Windows Version in UWP Apps (JS)

The one you can get through ver command, '10.0.16299.309', is the style what you get inside of your UWP app.

Sometimes, you might need to get the user's system operating system version when your app is running. It could be writing app launch log, or reporting crash. Of course, UWP applications can get current running Windows 10's version.

Use Windows.System.Profile.AnalyticsInfo.versionInfo.deviceFamilyVersion to get the specific version of Windows 10.

If the current version is 10.0.16299.309, deviceFamilyVersion shows "2814750835278133", a String. Windows provides OS version information to UWP in non-human comprehensive way, so you need to convert it manually. By the way, what is this number?

Let's run calculator and switch to programmer mode. Then paste the value (2814750835278133) we've got at deviceFamilyVersion in decimal. Switching to the hexadecimal, the number will look like four part divisible(each 16 bits) number. In this case, we get A 0000 3FAB 0135. Now, convert each parts into decimal. First one will be 10, second one will be 0, third one will be 309, and the last one will be 309. Joining each one with dot will be 10.0.16299.309 - it's the version of Windows RS3!

Now, calculating this process programmatically should be easy too. Use Bitwise AND (&) operator and bitwise right shift operator to extract specific digits, and switch to decimal. Check how to do this specifically here.

 

JavaScript needs different method to calculate?

C++ or C#, VB based apps just need to follow above linked page to bitwise calculation. JavaScript can do same thing, but unexpected result will come. Executing this (version & 0xFFFF000000000000) >> 48 operation, you would expect 10 but result is 0. Why this is happening?

For a simple test, I've made an arbitary version (10.7.16299.309), and converted to WinRT API style decimal number (2814780900049205). Then I've bitwise shift calculated in JavaScript console. The first four digits(48~63 bit) and the second four digits(32~47 bit) should returned 10 and 7, but it was both 0. On the other hand, the third four digits(16~31 bit) returned an expected result, 16299.

As you may know, JavaScript has only one type of number data, Number. All numbers are stored in 64 bit double precision floating point number, and it's 0 to 51 bit is for fraction, 52 to 62 bit is for exponent, and the last address 64 bit is for sign. Thus, JavaScript can calculate without any data loss with maximum number ±9007199254740991(001F FFFF FFFF FFFF). Bigger or smaller than this number will lead to unexpected calculation result.

Since JavaScript uses from 0 bit to 52 bit for fraction information, JS interpreter will truncate number bigger than 32 bit when calculating bitwise operation. The example above shows this phenomenon. But don't worry! There's a workaround to bypass this problem.

JavaScript
var current = Windows.System.Profile.AnalyticsInfo.versionInfo.deviceFamilyVersion;
var currentUlong = parseInt(current).toString(16),
    bit00 = parseInt(currentUlong.slice(currentUlong.length - 4), 16),
    bit16 = parseInt(currentUlong.slice(currentUlong.length - 8, currentUlong.length - 4), 16),
    bit32 = parseInt(currentUlong.slice(currentUlong.length - 12, currentUlong.length - 8), 16),
    bit48 = parseInt(currentUlong.slice(0, currentUlong.length - 12), 16);
return [bit48, bit32, bit16, bit00].join(".");

By calculating this method, you'll get the entire version information without losing any data. Let's take a walkthrough line by line.

First, we assign raw data of Windows version to current. It was provided in String, not Number so we're convert to integer by parseInt(current). Then, we make another variable named currentUlong, which will have hexadecimal form of current in String. If the Windows version is 10.0.16299.309, the currentUlong will have a string "a00003fab0173".

Next, we declare four variables, each named with bit00, bit16, bit32, bit48.

  • bit00 will have a decimal form of Number, converted from sliced String of currentUlong's 1st character to 4th character and treated as hexadecimal (same as (version & 0xFFFF000000000000) >> 48).
  • bit16 will have a decimal form of Number, converted from sliced String of currentUlong's 5th character to 8th character and treated as hexadecimal (same as (version & 0x0000FFFF00000000) >> 32).
  • bit32 will have a decimal form of Number, converted from sliced String of currentUlong's 9th character to 12th character and treated as hexadecimal (same as (version & 0x00000000FFFF0000) >> 16).
  • bit48 will have a decimal form of Number, converted from sliced String of currentUlong's 13th character to 16th character and treated as hexadecimal (same as version & 0x00000000FFFF).

On the last line, a new Array containing bit48, bit32, bit16, bit00 will be created and the new String joining each element of array will be returned. The return value will be "10.0.16299.309", if you're running Windows 10 RS3. Now you've got the version of currently running Windows version, so all you need to do now is use at where you need.