Getting ASP.NET version used by IIS Web Site

It is easy to get the ASP.NET version installed at the machine level. But it took me some time to figure out ( with the help of others , of course) to get the ASP.NET version used by a given web site in IIS , since side by side is supported by .NET and admin would be able to pick the ASP.NET version from IIS Manager and change it at any time at a granular level.

Here is the code snippet & help I got

String sWebSite = “w3svc/1”
DirectoryEntry site = new DirectoryEntry("IIS://localhost/" + sWebSite + @"/Root");
           
            

                    PropertyValueCollection vals = site.Properties["ScriptMaps"];
                    foreach (string val in vals)
                    {
                        if (val.StartsWith(".aspx"))
                        {
                            string version = val.Substring(val.IndexOf("Framework") + 10, 9);
                            MessageBox.Show(String.Format("ASP.Net Version on virtual server is {0}", version));

                        }

                    }
The idea here is to use the  System.DirectoryServices classes which will use IIS ADSI provider.
You need to read the ScriptMap property at your application path. For example if you wanna see the asp.net version for the default web site - read the ScriptMap at LM/W3SVC/1/ROOT/. The script map property is an array of strings. If the app supports asp.net one of those strings will be a mapping of the aspx file extension to the asp.net handler which will a the full path to a DLL. The path will be something like %windir%/Microsoft.NET/Framework/<asp.net ver>/aspnet_isapi.dll. You can get the version out of this string with some simple parsing.

Other approaches from scripts include

1) Running “cscript adsutil.vbs enum w3svc/filters”  (without quotes) from command prompt should list the filters in IIS metabase. You need to change the current directory to c:\inetpub\adminscripts before running this command.

2) %windir%\microsoft.net\framework\v2.0.50902\ASPNET_REGIIS.EXE –lk