使用JavaScript检测.NET Framework 3.5安装状态

作者:拾光时间:2026-10-23 12:09:45浏览:118

本文介绍一个基于HTML和JavaScript的检测脚本,用于判断系统中是否已安装.NET Framework 3.5。脚本通过解析浏览器用户代理字符串中的.NET CLR版本,并与目标版本3.5.0.0进行比对,最终在页面显示具体的检测结果及用户代理信息。

使用JavaScript检测.NET Framework 3.5安装状态

本文提供一个基于HTML和JavaScript编写的脚本,帮助管理员判断目标计算机上是否已安装.NET Framework 3.5。


具体实现代码如下:

<HTML>
<HEAD>
<TITLE>Test for the .NET Framework 3.5</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8" />
<SCRIPT LANGUAGE="JavaScript">
<!-- 
var dotNETRuntimeVersion = "3.5.0.0";
function window::onload()
{ 
if (HasRuntimeVersion(dotNETRuntimeVersion))
{ 
result.innerText =
"This machine has the correct version of the .NET Framework 3.5."
} 
else
{ 
result.innerText =
"This machine does not have the correct version of the .NET Framework 3.5." +
" The required version is v" + dotNETRuntimeVersion + ".";
} 
result.innerText += "\n\nThis machine's userAgent string is: " +
navigator.userAgent + ".";
} 
// 
// Retrieve the version from the user agent string and
// compare with the specified version.
// 
function HasRuntimeVersion(versionToCheck)
{ 
var userAgentString =
navigator.userAgent.match(/.NET CLR [0-9.]+/g);
if (userAgentString != null)
{ 
var i;
for (i = 0; i < userAgentString.length; ++i)
{ 
if (CompareVersions(GetVersion(versionToCheck),
GetVersion(userAgentString[i])) <= 0)
return true;
} 
} 
return false;
} 
// 
// Extract the numeric part of the version string.
// 
function GetVersion(versionString)
{ 
var numericString =
versionString.match(/([0-9]+)\.([0-9]+)\.([0-9]+)/i);
return numericString.slice(1);
} 
// 
// Compare the 2 version strings by converting them to numeric format.
// 
function CompareVersions(version1, version2)
{ 
for (i = 0; i < version1.length; ++i)
{ 
var number1 = new Number(version1[i]);
var number2 = new Number(version2[i]);
if (number1 < number2)
return -1;
if (number1 > number2)
return 1;
} 
return 0;
} 
--> 
</SCRIPT>
</HEAD>
<BODY>
<div id="result" />
</BODY>
</HTML>

当脚本成功在用户代理字符串中搜索到“.NET CLR”相关版本信息时,页面将显示如下状态消息:


This machine has the correct version of the .NET Framework 3.5.


This machine's userAgent string is: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; InfoPath.2; .NET CLR 3.0.590; .NET CLR 3.5.20726; MS-RTC LM 8).


若未找到匹配版本,则显示如下提示:


This machine does not have the correct version of the .NET Framework 3.5. The required version is v3.5.0.0.


This machine's userAgent string is: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; InfoPath.2; .NET CLR 3.0.590; MS-RTC LM 8).

作者最新文章