2009/03/28

Detect .Net Framework (1/19/2010 updated)

Sometimes you need to detect clients' environment in order to run your .net winform or webform application. I ever thought about using the System.Environment.Version, but if user doesn't have any .net framework installed on his/her machine, how can he/she run this code to get the .net version? It turns out that we have to use other technologies to detect the .net framework (like search user's registry or system folders ... etc).
Here is what I found on the Microsoft website, and I did some minor changes on it. You can copy the following code and save it as "DetectDotNet.html".
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
 <head>
  <title>Test for NET Framework</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <script type="text/javascript" language="JavaScript">
  //you can change the version that you want to detect
  var RequiredFXVersion = "2.0.50727";

  function check()
  {
   var foundVer = CheckRequiredFXVersion(RequiredFXVersion);
   if (foundVer != null)
   {
    result.innerHTML = "This computer has the correct version of the .NET Framework: " + foundVer + "." + "<br/>"
     + "This computer's userAgent string is: " + navigator.userAgent + ".";
   }
   else
   {
    result.innerHTML = "This computer does not have the correct version of the .NET Framework.<br/>"
     + "<a href='http://msdn.microsoft.com/windowsvista/default.aspx'>Click here</a> "
     + "to get .NET Framework 3.0 now.<br>"
     + "This computer's userAgent string is: " + navigator.userAgent + ".";
   }
  }

  //
  // Retrieve available versions from the user agent string
  // and check if any of them match the required version.
  //
  function CheckRequiredFXVersion(requiredVersion)
  {
   var userAgentString = navigator.userAgent.match(/\.NET CLR[ .][0-9.]+/g);
   if (userAgentString != null)
   {
    var i;
    for (i = 0; i < userAgentString.length; ++i)
    {
     var ver = userAgentString[i].slice(9);
     if (CheckVersion(requiredVersion, ver))
      return ver;
    }
   }
   return null;
  }

  //
  // Check if a specific version satisfies the version requirement.
  //
  function CheckVersion(requiredVersion, ver)
  {
   requiredVersion = requiredVersion.split(".");
   ver = ver.split(".");

   // Major versions must match exactly.
   if (requiredVersion[0] != ver[0])
    return false;

   // Minor/build numbers must be at least the required version.
   var i;
   for (i = 1; i < requiredVersion.length && i < ver.length; i++)
   {
    if (new Number(ver[i]) < new Number(requiredVersion[i]))
     return false;
   }
   return true;
  }
  </script>
 </head>
 <body onload="javascript:check();">
  <div id="result" />
 </body>
</html>
So next time you can just send this simple html file to clients and ask them to run it under IE.


Updated: 1/19/2010
A better way to check .Net Version: Here.

No comments:

Post a Comment