Best Way To Determine If .NET 3.5 Is Installed

2019-03-15 01:20发布

I need to programatically determine whether .NET 3.5 is installed. I thought it would be easy:

<% Response.Write(Environment.Version.ToString()); %>

Which returns "2.0.50727.1434" so no such luck...

In my research I have that there are some rather obscure registry keys I can look at but I'm not sure if that is the route to go. Does anyone have any suggestions?

9条回答
叛逆
2楼-- · 2019-03-15 01:46

@Kev, really like your solution. Thanks for the help.

Using the registry the code would look something like this:

RegistryKey key = Registry
        .LocalMachine
        .OpenSubKey("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.5");
return (key != null);

I would be curious if either of these would work in a medium trust environment (although I am working in full trust so it doesn't matter to what I am currently working on).

查看更多
Ridiculous、
3楼-- · 2019-03-15 01:46

@komradekatz, your solution below from MSDN for convenience for others looking into this. I do not like this solution because it uses the user agent to determine the version. This is not viable for what I need (I am writing a class library that needs to know whether .NET 3.5 is installed). I also question how reliable this solution may prove to be.

<%@ Page Language="C#" %>

<!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 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>

On my machine this outputs:

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 3.0.04506; InfoPath.2; .NET CLR 1.1.4322; .NET CLR 3.5.21022; Zune 2.5).

查看更多
甜甜的少女心
4楼-- · 2019-03-15 01:51

That is because technically .NET 3.5 is an extension of the 2.0 framework. The quickest way is to include an assembly from .NET 3.5 and see if it breaks.

System.Web.Extensions

Is a good assembly that is only included in version 3.5. Also it seems that you are using ASP.NET to run this check, this really limits you because you will be unable to check the file system or the registry running in the protected mode of ASP.NET. Or you can always problematically try loading an assembly from the GAC that should only be in .NET 3.5, however you may run in to problems with permissions again.

This may be one of those times where you ask your self "What am I trying to accomplish?" and see if there are alternative routes.

查看更多
Evening l夕情丶
5楼-- · 2019-03-15 01:55

You could try:

static bool HasNet35()
{
    try
    {
        AppDomain.CurrentDomain.Load(
            "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
        return true;
    }
    catch
    {
        return false;
    }
}

@Nick: Good question, I'll try it in a bit.

Kev

查看更多
乱世女痞
7楼-- · 2019-03-15 02:00

One option is to detect 4.0 using the version string:

    Environment.Version.CompareTo(new Version(4, 0));

then since 2.0 and 2.5 share a CLR version number, these need to be distenguished by checking the registry. Since those versions are released already, the strings to look for are known.

查看更多
登录 后发表回答