There was a question that surfaced on the Arizona .NET User Group list about determining which versions of .NET are on a system, and which service packs have been applied.  It sounded like a fun challenge, so I spent a few minutes this afternoon learning about how to dig up that data.  Definitely the most helpful information came from this post in Aaron Stebner's blog, which was pointed out by my friend Tim Heuer.  Aaron's sample code is only available in native C++ and VB.NET, so I decided to write a simplified version in C#.  Here is the pertinent code to dig up that information from the registry, which will work for anything that has “.NET Framework“ in its name:

// (Note that lbInstVersions is a listbox placed on a WinForm or Web Form.)
string componentsKeyName="SOFTWARE\\Microsoft\\Active Setup\\Installed Components",
   friendlyName,
   version;
// Find out in the registry anything under:
//    HKLM\SOFTWARE\Microsoft\Active Setup\Installed Components
// that has ".NET Framework" in the name
RegistryKey componentsKey=Registry.LocalMachine.OpenSubKey(componentsKeyName);
string[] instComps=componentsKey.GetSubKeyNames();
foreach(string instComp in instComps)
{
   RegistryKey key=componentsKey.OpenSubKey(instComp);
   friendlyName=(string)key.GetValue(null); // Gets the (Default) value from this key
   if(friendlyName != null && friendlyName.IndexOf(".NET Framework")>=0)
   {
      // Let's try to get any version information that's available
      version=(string)key.GetValue("Version");
      // If you want only the framework info with its SP level and not the
      // other hotfix and service pack detail, uncomment this if:
      //    if(version!=null && version.Split(',').Length>=4)
      lbInstVersions.Items.Add(friendlyName+(version!=null?(" ("+version+")"):""));
   }
}

On my system running in a WinForms app it brings up these results:

It should work on a Win32 or Win64 system to detect versions of the framework installed either as an OCX or from an MSI.  So far (and hopefully this will always be), only true versions of the .NET Framework show up with this filter.

The commented out IF statement in the code above (next to the last line) just tests how many pieces come back from the version number when you Split() on a comma.  If it's four or more, it assumes it's not a hotfix or service pack, and thus it's a true framework installation.  The fourth number in the version number is in fact always the current service pack level.

From here it would be easy to make a class to describe framework versions, with major and minor numbers.  Also from there an IComparer so it's sortable in an array or SortedList.  The sky's the limit.  But really something like this becomes useful before installation of a product, so a VBScript version would be handy.  Maybe I'll write one of those next if anyone would find it useful.

 


Feedback

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Lorin:

Thanks for this code. I found this while searching Google on how to determine the latest installed version of the .NET Framework. I added some custom code to compare the strings to determine which was greater. I wanted to share some code on how to determine the install directory of the latest version of the .NET Framework:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
class Program
{
[DllImport("mscoree.dll")]
private static extern int GetCORSystemDirectory(
[MarshalAs(UnmanagedType.LPWStr)]StringBuilder pbuffer,
int cchBuffer, ref int dwlength);

static void Main(string[] args)
{
GetClrInstallationDirectory();
}

private static void GetClrInstallationDirectory()
{
int MAX_PATH = 260;
StringBuilder sb = new StringBuilder(MAX_PATH);
GetCORSystemDirectory(sb, MAX_PATH, ref MAX_PATH);
Console.WriteLine(sb.ToString());
while(Console.Read() != 'q') ;
}

}
}



4/6/2006 4:42 AM | ike

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Of Course , you guy , two method done well .
And very useful.s 5/30/2006 9:07 PM | ms44cn

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Even i needed to do something similar..

Came across following property:

Environment.Version
It returns the latest version of the CLR installed.

Something as simple as that.
It also contains more properties to find further details like major, minor revisions etc

The Environment Class is pretty interesting and contains many other methods for finding other system details.

3/30/2007 12:05 PM | Sameer Palande

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Except that the .NET framework 3.0 does not show a version of 3, but a version of 2.

This is just to create a little confusion.

You can read more about this here:
http://blogs.msdn.com/brada/archive/2006/06/11/627128.aspx
5/31/2007 1:06 PM | Marco

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Hopefully this won't post a bunch of times - When I try to press the Post button - nothing seems to happen - so I've tried a few times now! ;)

This seems to work and all, as such... and thanks for the code - it's a start! :)

But I was looking for something that would list *all* the version*s* - as per the title: "C# code to determine which versions of the .NET Framework are installed..."

Either of the two example code segments tells me I have .NET 2.0 - but I really need something that will tell me I have .NET v1.0, v1.1, v2.0, and v3.0 32bit, and v2.0 and v3.0 64bit... in other words, code that will return a full list of the .NET versions installed.

Seen that anywhere? 8/6/2007 10:48 AM | unRheal

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Environment.Version as was correctly stated returns the CLR version. That is different to the .NET framework version.

Frameworks 1 and 1.1 came with CLR version 1. Frameworks 2 and upwards use CLR version 2.

So it's not wrong, just not useful if you are trying to determine the version of the framework installed! 3/5/2008 9:02 AM | andy

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Haven't seen this before 7/4/2009 11:32 PM | Driver Scanning

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Wow really very nice and good information you share here. I read your entire post and really superb information you share here on funny stuff thanks for your information. 9/10/2009 6:49 PM | funny stuff

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

If you're ever in Phoenix may I wholeheartedly recommend 10/17/2009 11:54 PM | Tiffany co Rings

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Now, you don’t have to worry about getting a failing mark simply because you have been accused of plagiarizing someone else’s work. 11/6/2009 11:17 PM | DDos Protection

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Hey,
I really liked your blog! Keep the articles coming I am going to pass your site to others.

Business Plan
12/21/2009 1:26 AM | Marketing Plan

# re: net 3.5 svc pk1 framework install failed error 1603

Maybe you could help me - I have purchased the turbotax software and unable to download it due to error 1603. I have spent countless hours with turbo's rep and none could determine the problem nor help me to fix it.

would you have any suggestions?

Thanks

p.s. If this is not the appropriate place to seek help - my apologies. 1/13/2010 5:42 PM | Frackville

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs



wow tnx i got all what i wan't from ur site
but what about ,the version of the .net frame work that my program or application built ,i think it is in the config file . tell me how to get
i wan't to check the version of .net frame work of my app and the version installed in my pc.
tnx
c#
regards .. i will wait ur answer 1/20/2010 1:40 AM | michael

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

When using this slimming patches strategy in that context it makes weight loss possible. It is not difficult to see, now is it. This is true for all intents and purposes. That will supply you with pills wisdom you need.

I'm using it right now. I have some neat slimming patches memorabilia. One must remember of ideal timing in terms of health. I guess it's a good thing I got into diet back in 2005. What is this, 20 questions? Silver patches pays dividends for days to come. If you'll be working like myself, I'll write another story slimming patches relevant to later this month for you to enjoy.
1/24/2010 9:32 AM | Jordan

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Hi, quite informative stuff here. Have any of you guys came across the problem that you have made an application (actually a VSTO add-in for Word in my case) that relies on the .Net Framework version 3.5, where you also have included a setup project to make MSI installation files. When trying to install the application on a Win 7 computer with .Net Framework version 4.0 installed, the installation wizard for .Net Framework 3.5 appears!? My assumption/understanding was that version 4.0 of the .Net framework included 3.5. Is this incorrect, or is the problem that the mechanism (in Visual Studio set up as a prerequisite in the setup project properties) detecting framework version does only exact check on version (3.5) not knowing that version 4.0 is backward compatible and thus will be adequate for the installation? Any tips on how to resolve this issue would be greatly appreciated!
Cheers, Dag.
2/17/2010 3:50 AM | Dag

# wwww

would you have any suggestions? 6/6/2010 8:43 AM | linkslondon

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Thanks for the nice blog. It was very useful for me. Keep sharing such ideas in the future as well. This was actually what I was looking for, and I am glad to came here! Thanks for sharing the such information with us. 6/9/2010 6:04 AM | search marketing group case stud

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

thank youfdfdf 7/27/2010 12:34 AM | gucci bags

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

abercrombie fitch This quarter shows a retro abercrombie clothing style main complex abercrombie accessories decorative abercrombie outlet style. It is the abercrombie sale popular, custom, theater and other elements blend in with elegant and beautiful way to show a similar tale plot, great tension. The typical Gothic architecture reflected in fashion, the broad shoulders, lace, tassels and other use, select the brocade, double palace silk, chiffon, linen and other fabrics like silk, combined with linen, with natural materials to restore design the most primitive, natural . Rococo and Baroque style, these two distinct styles, lines, smooth contours, in today's society is still widely popular. Closer to the works of some master of architectural style, choose the hard texture of the brocade, clothing lines make more tough. Main colors are: light colors popular this summer, timeless classic black, powerful and thick dark purple, with the abercrombie and fitch usa visual impact of the abercrombie store product in red, soft silver pink cheap abercrombie.

http://www.aberclombie.com/abercrombie-shop.html
8/10/2010 10:29 PM | aberclombie

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Thank you very much for the supreme post provided! I was looking for this entry for a long time, but I wasn’t able to find a trusted source. 8/20/2010 10:40 PM | clothing online

Post a comment





 

 

News


Welcome to my blog.
Here's what we've got on the menu today:

Tag Cloud


Article Categories

Archives

Post Categories

Image Galleries

Syndication: