次の方法で共有


Updated sample .NET Framework detection code that does more in-depth checking

I previously posted some sample code to detect the version(s) and service pack levels of the .NET Framework that are installed on a computer (here and here). The original version of the sample code that I wrote queries the officially documented registry values that are used to detect the presence of each specific version of the .NET Framework.

Since I posted this sample code, I have heard feedback from some customers who are including the .NET Framework as part of their setup packages. They indicated that sometimes this code reports false positives - in other words, the sample returns true for a specific version of the .NET Framework but it isn't actually installed on the system. I have seen this a couple of times in the past as well, and the root cause was that some of the registry entries used to detect the .NET Framework were orphaned on the system after an uninstall or OS reinstall scenario.

In order to help address this type of issue, I've created a new version of the sample code that adds some new checks to help guard against orphaned registry values. The logic it uses is to query the registry values like the previous sample used to, and then to supplement that with an additional check that loads mscoree.dll and uses some of its APIs to query for the presence of specific versions of the .NET Framework. The underlying algorithm for this mscoree.dll-based check came from Junfeng Zhang and from a sample published on MSDN.

This algorithm should prove more reliable in detecting whether or not a specific version of the .NET Framework is installed on the system because it does not rely solely on the registry. In addition, it provides the side benefit of performing a quick health check for the .NET Framework itself because it attempts to invoke some APIs in the runtime.

The new sample code contains the following specific changes from the previous version that I published:

  • Added the CheckNetfxVersionUsingMscoree and GetProcessorArchitectureFlag functions
  • Added logic in WinMain to call CheckNetfxVersionUsingMscoree in addition to the original calls to check .NET Framework registry data
  • Updated the code to use strsafe.h functions for string manipulations

You will need the following in order to compile this sample:

Please let me know if you have any trouble building or running this sample or incorporating it into your product setup logic.

<update date="5/29/2009"> Fixed broken link to the sample code. </update>

Comments


.NET Framework Versions Installed

.NET Framework 1.0 service pack 3 is installed. .NET Framework 1.1 service pack 1 is installed. .NET Framework 2.0 service pack 2 is installed. .NET Framework 3.0 service pack 2 is installed. .NET Framework 3.5 service pack 1 is installed.

OK  

Cheers! David Rogers

  • Anonymous
    December 06, 2008
    Hi DavidWRogers - Those sound like helpful changes, and I'm glad you found the sample useful. Also, note that there is now a web-based version of this check available at http://www.smallestdotnet.com.  It isn't exactly the same - it only lists the most recent version you have installed and what the smallest download is to get you updated to the .NET Framework 3.5 SP1, but this type of checking might be useful in some scenarios.  There is also more detail about what that web page does at http://www.hanselman.com/blog/SmallestDotNetOnTheSizeOfTheNETFramework.aspx, and in that page there is a script snippet you can embed in your web page to provide equivalent functionality.

  • Anonymous
    June 14, 2009
    PingBack from http://rapid-dev.net/2009/06/is-there-an-easy-way-to-check-net-framework-verison-using-c/

  • Anonymous
    December 28, 2009
    I have developed an ActiveX control using c# and .net framework 2.0. If this has to be deployed on a client m/c and the m/c does not have .net framework what should we do? I have used the following code to detect the framework since I cannot read the registry in the client m/c from aspx page. When I invoke the method it detects .net framework 2.0 is not installed. The user clicks on a link from the main page and goes to the popup window to install the .net framework 2.0. if (Request.Browser.Browser.ToUpper() == "IE"){ Version[] versionArray = Request.Browser.GetClrVersions(); foreach (Version version in versionArray) { if (version.ToString().StartsWith("2.") || version.ToString().StartsWith("3.")) { bldotNetFramework2 = true; break; } } if (!bldotNetFramework2) { Response.Redirect(sdlURL + "/Home.aspx"); } When the user comes back to  the main page and click refresh it still says .net framework 2.0 not installed. But If I close and re-open the browser then main page detects .net framework 2.0. How do we resolve this? Any help would be appreciated.

  • Anonymous
    December 28, 2009
    Hi Nitingel - I'm not sure there is anything better you can do in this scenario.  The .NET Framework installer adds a registry value during its setup process that updates the browser version information, so that update won't be visible until you re-open the browser.  Applications that require Silverlight have the same type of issue when they request that their users install Silverlight - the Silverlight installer tells them that they need to re-open the browser.  You may want to add that type of message to your pop-up window to handle this scenario.

  • Anonymous
    April 10, 2011
    Thanks for the src! http://www.asoft.be also has a nice one that I use and it's kept uptodate. There is no source but it's the one that I package to customers. It's called .NET version detector. My 2cts

  • Anonymous
    August 14, 2011
    Hey Oliver, thanks for the post. It was most helpful. Kevin

  • Anonymous
    January 31, 2013
    The links for the "new sample code" seem to be broken.. :(

  • Anonymous
    January 31, 2013
    Hi George - The links appear to be working fine for me.  What error do you see when you try to click on the links?

  • Anonymous
    November 01, 2015
    The GetRequestedRuuntimeInfo API  has been deprecated in the .NET Framework 4