Best Way To Determine If .NET 3.5 Is Installed

前端 未结 9 1596
时光取名叫无心
时光取名叫无心 2021-02-05 12:43

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

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


        
相关标签:
9条回答
  • 2021-02-05 13:25

    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

    0 讨论(0)
  • 2021-02-05 13:28

    If you want to require a specific version of .net to be installed and can control the distribution of your application, you should really use ClickOnce. It allows you to specify the minimum required version of the .Net framework that should be installed, and it will only check when it is being installed so that all your subsequent startups are not impeded by an unnecessary check.

    Also, with ClickOnce you get updating for free. Why wouldn't somebody want to use it?

    To set up a ClickOnce application, just right click on the project within Visual Studio and go to the Publish Settings. This will create a special build of your application that you can place on your website. When users download the program, the installer will check for any prerequisites like .Net for you.

    0 讨论(0)
  • 2021-02-05 13:33

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

    0 讨论(0)
  • 2021-02-05 13:36

    Without any assembly loading and catching exceptions (which is slow), check for class API changes between 2.0 and 3.5. Mono Class Status is very helpful for this. For example you could check for GC.Collect Method (Int32, GCCollectionMode) which is in mscorlib and was added in 3.5 .

    0 讨论(0)
  • 2021-02-05 13:38

    A good resource I found:

    http://www.walkernews.net/2008/05/16/how-to-check-net-framework-version-installed/

    0 讨论(0)
  • 2021-02-05 13:40

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

    0 讨论(0)
提交回复
热议问题