Figuring out the IIS Version for a given OS in .NET Code

Posted by Rick Strahl on West-Wind See other posts from West-Wind or by Rick Strahl
Published on Mon, 19 Sep 2011 02:50:56 GMT Indexed on 2011/11/11 17:53 UTC
Read the original article Hit count: 665

Filed under:
|

Here's an odd requirement: I need to figure out what version of IIS is available on a given machine in order to take specific configuration actions when installing an IIS based application. I build several configuration tools for application configuration and installation and depending on which version of IIS is available on IIS different configuration paths are taken. For example, when dealing with XP machine you can't set up an Application Pool for an application because XP (IIS 5.1) didn't support Application pools. Configuring 32 and 64 bit settings are easy in IIS 7 but this didn't work in prior versions and so on.

Along the same lines I saw a question on the AspInsiders list today, regarding a similar issue where somebody needed to know the IIS version as part of an ASP.NET application prior to when the Request object is available.

So it's useful to know which version of IIS you can possibly expect. This should be easy right? But it turns there's no real easy way to detect IIS on a machine. There's no registry key that gives you the full version number - you can detect installation but not which version is installed.

The easiest way: Request.ServerVariables["SERVER_SOFTWARE"]

The easiest way to determine IIS version number is if you are already running inside of ASP.NET and you are inside of an ASP.NET request. You can look at Request.ServerVariables["SERVER_SOFTWARE"] to get a string like

Microsoft-IIS/7.5

returned to you. It's a cinch to parse this to retrieve the version number.

This works in the limited scenario where you need to know the version number inside of a running ASP.NET application. Unfortunately this is not a likely use case, since most times when you need to know a specific version of IIS when you are configuring or installing your application.

The messy way: Match Windows OS Versions to IIS Versions

Since Version 5.x of IIS versions of IIS have always been tied very closely to the Operating System. Meaning the only way to get a specific version of IIS was through the OS - you couldn't install another version of IIS on the given OS. Microsoft has a page that describes the OS version to IIS version relationship here:

http://support.microsoft.com/kb/224609

In .NET you can then sniff the OS version and based on that return the IIS version.

The following is a small utility function that accomplishes the task of returning an IIS version number for a given OS:

    /// <summary>
    /// Returns the IIS version for the given Operating System.
    /// Note this routine doesn't check to see if IIS is installed
    /// it just returns the version of IIS that should run on the OS.
    /// 
    /// Returns the value from Request.ServerVariables["Server_Software"]
    /// if available. Otherwise uses OS sniffing to determine OS version
    /// and returns IIS version instead.
    /// </summary>
    /// <returns>version number or -1 </returns>
    public static decimal GetIisVersion()
    {
        // if running inside of IIS parse the SERVER_SOFTWARE key
        // This would be most reliable
        if (HttpContext.Current != null && HttpContext.Current.Request != null)
        {
            string os = HttpContext.Current.Request.ServerVariables["SERVER_SOFTWARE"];
            if (!string.IsNullOrEmpty(os))
            {
                //Microsoft-IIS/7.5
                int dash = os.LastIndexOf("/");
                if (dash > 0)
                {
                    decimal iisVer = 0M;
                    if (Decimal.TryParse(os.Substring(dash + 1), out iisVer))
                        return iisVer;
                }
            }
        }

        decimal osVer = (decimal) Environment.OSVersion.Version.Major +
                ((decimal) Environment.OSVersion.Version.MajorRevision / 10);

        // Windows 7 and Win2008 R2
        if (osVer == 6.1M)
            return 7.5M;
        // Windows Vista and Windows 2008
        else if (osVer == 6.0M)
            return 7.0M;
        // Windows 2003 and XP 64 bit
        else if (osVer == 5.2M)
            return 6.0M;
        // Windows XP
        else if (osVer == 5.1M)
            return 5.1M;
        // Windows 2000
        else if (osVer == 5.0M)
            return 5.0M;

        // error result
        return -1M;                
    }
}

Talk about a brute force apporach, but it works.

This code goes only back to IIS 5 - anything before that is not something you possibly would want to have running. :-) Note that this is updated through Windows 7/Windows Server 2008 R2. Later versions will need to be added as needed. Anybody know what the Windows Version number of Windows 8 is?

© Rick Strahl, West Wind Technologies, 2005-2011
Posted in ASP.NET  IIS  
kick it on DotNetKicks.com

© West-Wind or respective owner

Related posts about ASP.NET

Related posts about iis