Click here to Skip to main content
15,867,308 members
Articles / Silverlight

How to Detect Installed Silverlight Version?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
23 Sep 2010CPOL3 min read 29.6K   4   5
How to detect installed Silverlight version?

Recently, I wanted to detect the Silverlight version for one of my ongoing R&D. I wanted to detect the version from batch file and depending upon that, I wanted to execute one file. Searching on the 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 the process of detecting the Silverlight installation status using JavaScript from the .hta file. Read it and at the end, if you have any suggestions to improve the steps, please let me know.

What is HTA File?

Let us first discuss about the .hta file. What is it? 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 an 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.

JavaScript
<script type="text/javascript" language="javascript">
   1:  
   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');
  10:  
  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();
  39:  
  40:         // return the version of the Silverlight.
  41:         return silverlightVersion;
  42:     }
</script>

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 an 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 conditions 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, it 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:

JavaScript
<script type="text/javascript">
   1:  
   2:     // get the Silverlight version
   3:     var silverlightVersion = GetSilverlightVersion();
   4:  
   5:     // if the retrieved version is -1, means detection failed.
   6:     if (silverlightVersion == -1) {
   7:         document.writeln("Unable to detect the Silverlight Version.");
   8:     }
   9:     // if the retrieved 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:     }
</script>

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:

HTML
<html>
<head>
    <title>Silverlight Version Tester</title>
    <script type="text/javascript" language="javascript">
   1:  
   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');
  10:  
  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();
  39:  
  40:             // return the version of the Silverlight.
  41:             return silverlightVersion;
  42:         }
  43:     
</script>

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

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

This article was originally posted at http://www.kunal-chowdhury.com/feeds/posts/default

License

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


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead 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 app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
GeneralIncomplete solution Pin
tornbladswe10-Dec-10 11:51
tornbladswe10-Dec-10 11:51 
GeneralNice article. Pin
Sushant Joshi25-Sep-10 5:44
Sushant Joshi25-Sep-10 5:44 
GeneralRe: Nice article. Pin
Kunal Chowdhury «IN»27-Sep-10 22:55
professionalKunal Chowdhury «IN»27-Sep-10 22:55 
GeneralMy vote of 5 Pin
Abhijit Jana24-Sep-10 1:50
professionalAbhijit Jana24-Sep-10 1:50 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»27-Sep-10 22:55
professionalKunal Chowdhury «IN»27-Sep-10 22: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.