Click here to Skip to main content
12,693,412 members (31,948 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked

How to detect installed Silverlight version?

, 23 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Recently, I wanted to detect the Silverlight version for one of my on going R&D. I wanted to detect the version from batch file and depending upon that I wanted to execute one file.

Recently, I wanted to detect the Silverlight version for one of my on going R&D. I wanted to detect the version from batch file and depending upon that I wanted to execute one file. Searching over internet I came to know that, HTML Application has some additional power for execution of any local file as it runs under fully trusted mode. I did more research on HTA files and successfully completed my basic analysis.

Here I will demonstrate you the process of detecting the Silverlight installation status using JavaScript from the .hta file. Read it and at the end, if you have any suggestion to improve the steps, please let me know.

What is HTA file?

Let us first discuss about the .hta file. What it is? HTA stands for HTml Application, by which you can run any script in fully trusted mode. You can read/write Registry values, you can do any file operation too. It is nothing but a HTML file. You can write HTML tags, JavaScripts, CSS Styles inside it as you do in HTML pages. But when you save it as a .hta file, the icon will change like an application icon. Then when you run the .hta file, it actually executes the file.

Detecting the Silverlight Version:

To detect the Silverlight version, you need to create the ActiveX Object of the AgControl using Scripting language like “JavaScript”. Once the object has been created, you can call the method IsVersionSupported() with proper parameter. This will return you the boolean value true/false. You must have to call the function in reverse chronological order of the parameter.

Have a look into the following JavaScript code to detect whether the Silverlight has been installed or not. If it is installed, it will return the latest Silverlight version.

<script type="text/javascript" language="javascript">
   2:     function GetSilverlightVersion() {
   3:         // initialize the silverlightVersion to -1.
   4:         var silverlightVersion = -1;
   5:         getSilverlightVersion = function () {
   6:             try {
   7:                 // create the ActiveX Object of AgControl.
   8:                 // This is the core of Silverlight runtime.
   9:                 var control = new ActiveXObject('AgControl.AgControl');
  11:                 // will execute if your latest Silverlight version is 4.
  12:                 if (control.IsVersionSupported("4.0")) {
  13:                     silverlightVersion = 4;
  14:                 }
  15:                 // will execute if your latest Silverlight version is 3.
  16:                 else if (control.IsVersionSupported("3.0")) {
  17:                     silverlightVersion = 3;
  18:                 }
  19:                 // will execute if your latest Silverlight version is 2.
  20:                 else if (control.IsVersionSupported("2.0")) {
  21:                     silverlightVersion = 2;
  22:                 }
  23:                 // if Silverlight version is not supported by your app,
  24:                 // set it as 0 (zero).
  25:                 else {
  26:                     silverlightVersion = 0;
  27:                 }
  28:                 control = null;
  29:             }
  30:             catch (e) {
  31:                 // if any exception while creating the ActiveX Object,
  32:                 // will set the silverlightVersion as -1.
  33:                 silverlightVersion = -1;
  34:                 alert("Unable to create the ActiveX Object from Browser window.");
  35:             }
  36:         }
  37:         // call to the inner function to detect the Silverlight.
  38:         getSilverlightVersion();
  40:         // return the version of the Silverlight.
  41:         return silverlightVersion;
  42:     }

Here comes the power of .hta file. If you run the code inside the browser window, it will not be able to create the ActiveX object and will throw exception. To do this, you need to run the code in fully trusted mode and HTML application will do the tricks for you as it runs in full trust mode. You can write more condition inside it to check other versions.

Showing the Silverlight version:

Now, once you get the Silverlight version, what will you do? You need to show some message to the user. Create another script code. If the detected version comes as “–1”, there must be some error detecting the Silverlight plug-in.

If the detected version comes as “0” (zero) from the above JavaScript code, means the Silverlight plug-in is not installed in the PC. In the other case, it will print the desired major version to the screen.

Look into the code:

<script type="text/javascript">
   2:     // get the Silverlight version
   3:     var silverlightVersion = GetSilverlightVersion();
   5:     // if the retrived version is -1, means detection failed.
   6:     if (silverlightVersion == -1) {
   7:         document.writeln("Unable to detect the Silverlight Version.");
   8:     }
   9:     // if the retrived version is 0, means Silverlight is not installed.
  10:     else if (silverlightVersion == 0) {
  11:         document.writeln("Silverlight is not installed in your PC.");
  12:     }
  13:     // show the Silverlight version.
  14:     else {
  15:         document.writeln("You are using Silverlight " + silverlightVersion);
  16:     }

The above code is based on your requirement. If you want to show a message rendered into the screen, you will use the document.writeln() method. If you want to show an alert message to the user, you will use the alert() method from the JavaScript. You can do other operation too based on the business requirement.

Here is the full code for your reference:

    <title>Silverlight Version Tester</title>

Hope this will give you some basic idea and based on that, you will be able to improve the detection logic. Share your feedbacks/suggestions to improve this logic.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kunal Chowdhury « IN »
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Technical Blog:
Twitter :

You may also be interested in...


Comments and Discussions

GeneralIncomplete solution Pin
tornbladswe10-Dec-10 12:51
membertornbladswe10-Dec-10 12:51 
GeneralNice article. Pin
Sushant Joshi25-Sep-10 6:44
memberSushant Joshi25-Sep-10 6:44 
GeneralRe: Nice article. Pin
KunalChowdhury27-Sep-10 23:55
mentorKunalChowdhury27-Sep-10 23:55 
GeneralMy vote of 5 Pin
Abhijit Jana24-Sep-10 2:50
mvpAbhijit Jana24-Sep-10 2:50 
GeneralRe: My vote of 5 Pin
KunalChowdhury27-Sep-10 23:55
mentorKunalChowdhury27-Sep-10 23:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 23 Sep 2010
Article Copyright 2010 by Kunal Chowdhury « IN »
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid