方法: .NET Framework 3.0 がインストールされているかどうかを確認する

管理者が Microsoft .NET Framework アプリケーションをシステムに配置するには、最初に、.NET Framework ランタイムが存在することを確認する必要があります。 ここでは、システムに .NET Framework が存在するかどうかを確認するための、HTML/JavaScript で記述されたスクリプトを示します。

メモメモ

Microsoft .NET Framework のインストール、配置、および検出の詳細については、「Deploying Microsoft .NET Framework Version 3.0」を参照してください。

".NET CLR" ユーザー エージェント文字列の検出

.NET Framework がインストールされるときに、MSI によって ".NET CLR" およびバージョン番号が UserAgent 文字列に追加されます。 次の例では、単純な HTML ページに埋め込まれたスクリプトを示します。 このスクリプトは、UserAgent 文字列を検索して、.NET Framework がインストールされているかどうかを判断し、検索の結果についてのステータス メッセージを表示します。

<HTML>
  <HEAD>
    <TITLE>Test for the .NET Framework 3.0</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8" />
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    var dotNETRuntimeVersion = "3.0.04425.00";
    
    function window::onload()
    {
      if (HasRuntimeVersion(dotNETRuntimeVersion))
      {
        result.innerText = 
          "This machine has the correct version of the .NET Framework 3.0: " 
          + dotNETRuntimeVersion
      } 
      else
      {
        result.innerText = 
          "This machine does not have the correct version of the .NET Framework 3.0."
      }
      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.0: 3.0.04425.00

This machine's userAgent string is: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 3.0.04425.00).

それ以外の場合は、次のようなステータス メッセージが表示されます。

This machine does not have correct version of the .NET Framework 3.0.

This machine's userAgent string is: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727).