共用方式為


如何:偵測是否已安裝 .NET Framework 3.5

系統管理員必須先確認 .NET Framework 3.5 執行時間存在,系統管理員才能在以 .NET Framework 3.5 為目標的系統上部署 Windows Presentation Foundation (WPF) 應用程式。 本主題提供以 HTML/JavaScript 撰寫的腳本,系統管理員可用來判斷系統上是否有 .NET Framework 3.5。

注意

如需安裝、部署及偵測 .NET Framework 的詳細資訊,請參閱 安裝適用于開發人員 的 .NET Framework。

範例

安裝 .NET Framework 3.5 時,MSI 會將 「.NET CLR」 和版本號碼新增至 UserAgent 字串。 下列範例顯示內嵌在簡單 HTML 頁面中的腳本。 腳本會搜尋 UserAgent 字串,以判斷是否已安裝 .NET Framework 3.5,並在搜尋結果上顯示狀態訊息。

注意

此腳本是針對 Internet Explorer 所設計。 其他瀏覽器可能不會在 UserAgent 字串中包含 .NET CLR 資訊。

<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).

另請參閱