views:

1778

answers:

2

How do I find out the .NET framework of an IIS virtual directory is using in C#. I need to display it to the user.

using System.DirectoryServices;
using System.IO;

private enum IISVersion
{
    IIS5,
    IIS6
}

private void ReadVirtualDirectory(string server, string directory, IISVersion version)
{
    string siteID = string.Empty;
    string path = string.Empty;

    switch(verison)
    {
        case IISVersion.IIS5:
            path = string.Format("IIS://{0}/W3SVC/1/Root", server);

            if(!string.IsNullOrEmpty(directory))
            {
                path = string.Concat(path, "/", directory);
            }

            break;

        case IISVersion.IIS6:
            path = string.Format("IIS://{0}/W3SVC", server);

            if(!string.IsNullOrEmpty(directory))
            {
                DirectoryEntry de = new DirectoryEntry(path);

                foreach(DirectoryEntry child in de.Children)
                {
                    if(child.Properties["ServerComment"].Value.ToString().ToLower() == directory)
                    {
                        siteID = child.Name;
                        break;
                    }
                }

                path = string.Concat(path, "/", siteID);

                de.Close()
                de = null;
            }

            break;
    }

    DirectoryEntry iis = new DirectoryEntry(path);

    //display iis properties here...
    //need to display if the virtual directory is running under .NET 1.1 or 2.0

    iis.Close();
    iis = null;       
}
+3  A: 

It might not be the prettiest method, but if you can grab and parse the output from aspnet_regiis.exe -lk, you'll get all the information you need. An example of the output from a SharePoint VHD:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_Regiis -lk
W3SVC/  1.1.4322.2407
W3SVC/1/ROOT/Reports/   2.0.50727.0
W3SVC/1/ROOT/ReportServer/      2.0.50727.0
W3SVC/1619303638/Root/  2.0.50727.0
W3SVC/1619303638/Root/_layouts/images/  1.1.4322.2407
W3SVC/1619303638/Root/_layouts/inc/     1.1.4322.2407
W3SVC/1720207907/root/  2.0.50727.0
W3SVC/1720207907/root/SharedServices1/  2.0.50727.0
W3SVC/1848312571/Root/  2.0.50727.0
W3SVC/1848312571/Root/_layouts/images/  1.1.4322.2407
W3SVC/1848312571/Root/_layouts/inc/     1.1.4322.2407
W3SVC/784543535/Root/   2.0.50727.0
W3SVC/784543535/Root/_layouts/images/   1.1.4322.2407
W3SVC/784543535/Root/_layouts/inc/      1.1.4322.2407
W3SVC/98413328/Root/    2.0.50727.0
W3SVC/98413328/Root/_layouts/images/    1.1.4322.2407
W3SVC/98413328/Root/_layouts/inc/       1.1.4322.2407
boflynn
It's.....beautiful!
alphabeat
+5  A: 

In IIS there's no hard and fast way to find out which ASP.NET version is used by a website or 'virtual' directory (website application folder - the one that has cogs as an icon).

The reason for this is that IIS and the metabase data only know about scriptmaps and not ASP.NET versions (after all ASP.NET is just another ISAPI application). One way to determine the version of ASP.NET is to use something like the following:

using System;
using System.DirectoryServices;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"IIS://Localhost/W3SVC/1/root/MyApp";
            Console.WriteLine(GetAspNetVersion(path));
        }

        private static string GetAspNetVersion(string path)
        {
            using (DirectoryEntry app = new DirectoryEntry(path))
            {
                PropertyValueCollection pvc = app.Properties["ScriptMaps"];

                foreach (string scriptMap in pvc)
                {
                    if (scriptMap.StartsWith(".aspx"))
                    {
                        string[] mapping = scriptMap.Split(',');
                        string scriptProcessor = mapping[1];

                        // The version numbers come from the path
                        // C:\Windows\Microsoft.NET\Framework\
                        // which will be present in the script processor 
                        // DLL path
                        if (scriptProcessor.Contains("v1.1.4322"))
                        {
                            return "1.1";
                        }

                        if (scriptProcessor.Contains("v2.0.50727"))
                        {
                            return "2.0";
                        }
                    }
                }
                throw new Exception("Unknown version ASP.NET version.");
            }
        }
    }
}

It's not ideal but hasn't failed our needs as a hoster for our provisioning applications/services. I suspect the IIS MMC plug-in performs a similar check behind the scenes. My own detailed examination of the metabase (Windows 2000 and Windows 2003 file based XML) reveals that there isn't a conspicuous property/attribute that stores a specific ASP.NET version number.

HTH
Kev

Kev
Thanks for the solution. I will have to modified it to get the framework version for each virtual directory in the web site.
Michael Kniskern