Tuesday, December 23, 2008

CLR version on client

Click once deployment requires CLR to be present on target machine to verify manifest. Following snippets help in detecting the same.

Snippets work with IE5+ and Firefox 3+ only. They fail with Chrome.

Server Side:

protected void Page_Load(object sender, EventArgs e)
{
bool clrOK = Request.Browser.ClrVersion > new Version(3, 0);
if (clrOK)
Response.Write("CLR Version : " + Request.Browser.ClrVersion);
else
Response.Write("This machine does not have Microsoft .NET Framework version 3.5 SP1. Install and try again.");
}



Client Side:



<script type="text/javascript">

function
DetectCLR(targetVersion) {
//Gets CLR version on IE5+ and Firefox 3+
var uagent = navigator.userAgent;

//Form regular expression
var clrRegExp = new RegExp(".NET CLR \\d.\\d.\\d{5}", "g");

//Get Matches
var clrversions = uagent.match(clrRegExp);

//Is CLR present
if (clrversions == null)
document.write("Please install Microsoft .Net Framework 3.5 SP1 and try again");

//Possible .NET CLR versions for v2 and above
//.NET CLR 2.0.50727
//.NET CLR 3.0.04506
//.NET CLR 3.5.21022
//.NET CLR 3.5.30729
var arraylength = clrversions.length;
var clrversiondetected;
for (var i = 0; i < arraylength; i++) {
//document.write(clrversions[i] + "</br>");
clrversiondetected = (clrversions[i] == targetVersion);
}


if (!clrversiondetected)
document.write("Please install Microsoft .Net Framework 3.5 SP1 and try again");
else
document.write("CLR Version : <b>" + targetVersion + "</b>");

return clrversiondetected;
}
</script>

No comments: