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

<pre><code>&lt;% Response.Write(Environment.Version.ToString()); %&gt;
</code></pre>

<p>Which returns "2.0.50727.1434" so no such luck...</p>

<p>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?</p>

[{'answer_id': 16187, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': 2, 'selected': False, 'text': '<p>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.  </p>\n\n<pre><code>System.Web.Extensions\n</code></pre>\n\n<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 16202, 'author': 'sestocker', 'author_id': 285, 'author_profile': 'https://Stackoverflow.com/users/285', 'pm_score': 1, 'selected': False, 'text': '<p>@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.</p>\n\n<pre><code>&lt;%@ Page Language="C#" %&gt;\n\n&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;\n\n&lt;HTML&gt;\n  &lt;HEAD&gt;\n    &lt;TITLE&gt;Test for the .NET Framework 3.5&lt;/TITLE&gt;\n    &lt;META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8" /&gt;\n    &lt;SCRIPT LANGUAGE="JavaScript"&gt;\n    &lt;!--\n    var dotNETRuntimeVersion = "3.5.0.0";\n\n    function window::onload()\n    {\n      if (HasRuntimeVersion(dotNETRuntimeVersion))\n      {\n        result.innerText = \n          "This machine has the correct version of the .NET Framework 3.5."\n      } \n      else\n      {\n        result.innerText = \n          "This machine does not have the correct version of the .NET Framework 3.5." +\n          " The required version is v" + dotNETRuntimeVersion + ".";\n      }\n      result.innerText += "\\n\\nThis machine\'s userAgent string is: " + \n        navigator.userAgent + ".";\n    }\n\n    //\n    // Retrieve the version from the user agent string and \n    // compare with the specified version.\n    //\n    function HasRuntimeVersion(versionToCheck)\n    {\n      var userAgentString = \n        navigator.userAgent.match(/.NET CLR [0-9.]+/g);\n\n      if (userAgentString != null)\n      {\n        var i;\n\n        for (i = 0; i &lt; userAgentString.length; ++i)\n        {\n          if (CompareVersions(GetVersion(versionToCheck), \n            GetVersion(userAgentString[i])) &lt;= 0)\n            return true;\n        }\n      }\n\n      return false;\n    }\n\n    //\n    // Extract the numeric part of the version string.\n    //\n    function GetVersion(versionString)\n    {\n      var numericString = \n        versionString.match(/([0-9]+)\\.([0-9]+)\\.([0-9]+)/i);\n      return numericString.slice(1);\n    }\n\n    //\n    // Compare the 2 version strings by converting them to numeric format.\n    //\n    function CompareVersions(version1, version2)\n    {\n      for (i = 0; i &lt; version1.length; ++i)\n      {\n        var number1 = new Number(version1[i]);\n        var number2 = new Number(version2[i]);\n\n        if (number1 &lt; number2)\n          return -1;\n\n        if (number1 &gt; number2)\n          return 1;\n      }\n\n      return 0;\n    }\n\n    --&gt;\n    &lt;/SCRIPT&gt;\n  &lt;/HEAD&gt;\n\n  &lt;BODY&gt;\n    &lt;div id="result" /&gt;\n  &lt;/BODY&gt;\n&lt;/HTML&gt;\n</code></pre>\n\n<p>On my machine this outputs:</p>\n\n<blockquote>\n  <p>This machine has the correct version\n  of the .NET Framework 3.5.</p>\n  \n  <p>This machine\'s userAgent string is:\n  Mozilla/4.0 (compatible; MSIE 7.0;\n  Windows NT 6.0; SLCC1; .NET CLR\n  2.0.50727; .NET CLR 3.0.04506; InfoPath.2; .NET CLR 1.1.4322; .NET\n  CLR 3.5.21022; Zune 2.5).</p>\n</blockquote>\n'}, {'answer_id': 16229, 'author': 'Kev', 'author_id': 419, 'author_profile': 'https://Stackoverflow.com/users/419', 'pm_score': 3, 'selected': True, 'text': '<p>You could try:</p>\n\n<pre><code>static bool HasNet35()\n{\n    try\n    {\n        AppDomain.CurrentDomain.Load(\n            "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");\n        return true;\n    }\n    catch\n    {\n        return false;\n    }\n}\n</code></pre>\n\n<p>@<a href="https://stackoverflow.com/questions/16178/best-way-to-determine-if-net-35-is-installed#16253">Nick</a>: Good question, I\'ll try it in a bit. </p>\n\n<p>Kev</p>\n'}, {'answer_id': 16299, 'author': 'sestocker', 'author_id': 285, 'author_profile': 'https://Stackoverflow.com/users/285', 'pm_score': 2, 'selected': False, 'text': '<p>@Kev, really like your solution. Thanks for the help.</p>\n\n<p>Using the registry the code would look something like this:</p>\n\n<pre><code>RegistryKey key = Registry\n        .LocalMachine\n        .OpenSubKey("Software\\\\Microsoft\\\\NET Framework Setup\\\\NDP\\\\v3.5");\nreturn (key != null);\n</code></pre>\n\n<p>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).</p>\n'}, {'answer_id': 16308, 'author': 'sestocker', 'author_id': 285, 'author_profile': 'https://Stackoverflow.com/users/285', 'pm_score': 2, 'selected': False, 'text': '<p>A good resource I found:</p>\n\n<p><a href="http://www.walkernews.net/2008/05/16/how-to-check-net-framework-version-installed/" rel="nofollow noreferrer">http://www.walkernews.net/2008/05/16/how-to-check-net-framework-version-installed/</a></p>\n'}, {'answer_id': 16328, 'author': 'sestocker', 'author_id': 285, 'author_profile': 'https://Stackoverflow.com/users/285', 'pm_score': 1, 'selected': False, 'text': '<p>Another interesting find is the presence of assemblies here:</p>\n\n<blockquote>\n  <p>C:\\Program Files\\Reference\n  Assemblies\\Microsoft\\Framework\\v3.5</p>\n</blockquote>\n\n<p>You\'d think Microsoft would build a check for "latest version" into the framework.</p>\n'}, {'answer_id': 33974, 'author': 'skolima', 'author_id': 3205, 'author_profile': 'https://Stackoverflow.com/users/3205', 'pm_score': 0, 'selected': False, 'text': '<p>Without any assembly loading and catching exceptions (which is slow), check for class API changes between 2.0 and 3.5. <a href="http://mono.ximian.com/class-status/2.0-vs-3.5/index.html" rel="nofollow noreferrer">Mono Class Status</a> is very helpful for this. For example you could check for <code>GC.Collect Method (Int32, GCCollectionMode)</code> which is in mscorlib and was added in 3.5 .</p>\n'}, {'answer_id': 1061245, 'author': 'Jared', 'author_id': 24841, 'author_profile': 'https://Stackoverflow.com/users/24841', 'pm_score': 1, 'selected': False, 'text': '<p>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 <a href="http://msdn.microsoft.com/en-us/library/t71a733d%28VS.80%29.aspx" rel="nofollow noreferrer">ClickOnce</a>. 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.</p>\n\n<p>Also, with ClickOnce you get updating for free. Why wouldn\'t somebody want to use it?</p>\n\n<p>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.</p>\n'}, {'answer_id': 1612249, 'author': 'Phil', 'author_id': 193962, 'author_profile': 'https://Stackoverflow.com/users/193962', 'pm_score': 1, 'selected': False, 'text': '<p>One option is to detect 4.0 using the version string:</p>\n\n<pre><code>    Environment.Version.CompareTo(new Version(4, 0));\n</code></pre>\n\n<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/16178', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/285/']