Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

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


:P
On this page:

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?

Posted in ASP.NET  IIS  

The Voices of Reason


 

Yoganathan Sivaram
September 18, 2011

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

Also you may use the Majorversion and MinorVersion values under the registry key SOFTWARE\Microsoft\InetStp.

Rick Strahl
September 18, 2011

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

@Yonaganathan - ah cool. You know how far back that's supported though? I can see it in 7 and 7.5, but haven't got a machine here to check for others...

Espen Johnsen
September 19, 2011

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

LINQPad 4 on Windows 8 shows "6.2.8102.0" for "Environment.OSVersion.Version".

But "Environment.OSVersion.Version.MajorRevision" returns "0"?

Alex Reitbort
September 19, 2011

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

I usually use a different approach. I add a special page to my application that returns to me specific information that i need, i.e username of apppool, IIS version, etc. Then in installer, after it copied the files, you can access this page using WebClient or something similar.

Do not forget the obvious stuff like limiting this page to only local requests, handling errors in page(usually .Net not configured on IIS) on installer level with some helpful messages, etc.

Mike McDonald
September 19, 2011

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

Rick, I see those registry keys in Windows 2003 and Windows XP, not sure about Windows 2000.

Rick Strahl
September 20, 2011

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

Another thought about the Registry keys is that they live under HKLM which is not accessible in medium trust or even in a limited rights desktop app.

Liming
September 21, 2011

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

Off topic, but I'm pretty interested in the optimization you are going for depends on which IIS it is during installation. Create application pool is neat... I'm guessing you also turn off Gzip compression if it's IIS 7 already, otherwise, turn on your custom Gzip compression.. anything else?

Thanks Rick, been reading your blog for years.. Gzip, custom globalization provider, paypal /authorize.net, used them all.

Tyson Swing
September 22, 2011

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

Have you thought of using the WMI provider? I've used this to discover .NET installed details, IIS metabase info, and various other MS products. The one down side is it can be a little messy depending on what your looking for, but the IIS shouldn't be too bad.

André
October 12, 2011

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


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024