38

How to detect IIS version using C#?

Update: I meant from a winapp (actually the scenario is developing a custom installer that wants to check the version of the installed IIS to call the appropriate api's)

Amr Elsehemy
  • 1,033
  • 4
  • 13
  • 24
  • Server-side or client side (e.g. calling an IIS web from a winforms app) ? I guess you mean server-side. – splattne Jan 15 '09 at 11:58
  • I meant from a winapp (actually the scenario is developing a custom installer that wants to check the version of the installed IIS to call the appropriate api's) should wrote it in the main question..(sorry).. – Amr Elsehemy Jan 15 '09 at 12:11

11 Answers11

37

Found the answer here: link text The fileVersion method dosesn't work on Windows 2008, the inetserv exe is somewhere else I guess.

public Version GetIisVersion()
{
    using (RegistryKey componentsKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\InetStp", false))
    {
        if (componentsKey != null)
        {
            int majorVersion = (int)componentsKey.GetValue("MajorVersion", -1);
            int minorVersion = (int)componentsKey.GetValue("MinorVersion", -1);

            if (majorVersion != -1 && minorVersion != -1)
            {
                return new Version(majorVersion, minorVersion);
            }
        }

        return new Version(0, 0);
    }
}

I tested it, it works perfectly on Windows XP, 7 and 2008

Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
ErTelis
  • 371
  • 3
  • 2
29

You can get this information from the SERVER_SOFTWARE variable. It will return the following:

Microsoft-IIS/5.0 (Windows 2000)
Microsoft-IIS/5.1 (Windows XP)
Microsoft-IIS/6.0 (Windows 2003 Server)

etc.

If you're using ASP.NET, you can get this string via

Request.ServerVariables["SERVER_SOFTWARE"];

EDIT: It seems that you will have to query the registry to get this information. Take a look at this page to see how.

Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
11

This is how i do it.

FileVersionInfo verinfo = FileVersionInfo.GetVersionInfo(System.Environment.SystemDirectory + @"\inetsrv\inetinfo.exe");

//Tip... look at verinfo.MajorVersion.
Jesper Palm
  • 7,170
  • 31
  • 36
  • 1
    This is throwing a FileNotFoundException on Windows2008R2 (even when the file is there). Looks like a file permission issue. – Ignacio Soler Garcia Jun 18 '12 at 13:19
  • The file is not found by a 32 bits process on a 64 bits server because inetsrc\inetinfo.exe does not exist in the SysWOW64 directory. The registry method of jlafay works in all cases because the registry value is present in both the 32 bits and 64 bits sections of HKLM. – Berend Engelbrecht Jul 15 '14 at 15:05
7

U can find it in the registry.

Up to IIS version 6 you can find it here:

HKLM\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters

Since version 7 here:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp

MajorVersion MinorVersion

Xn0vv3r
  • 17,766
  • 13
  • 58
  • 65
  • IIS 7 also has an entry in HKLM\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters, at least on Windows7 32 bit. (Major version 7, minor version 5). But this key appears not to be availalbe on Windows Server 2008 (IIS7) anymore. – Stefan Steiger Sep 07 '10 at 07:53
4

In .NET 4.5

HttpRuntime.IISVersion
Robert
  • 5,278
  • 43
  • 65
  • 115
h0ckey09
  • 89
  • 7
  • Self-explanatory... How to detect IIS version using C#? Use httpRuntime.IISVersion. – h0ckey09 Aug 04 '14 at 20:16
  • As I understand the question, the OP is looking for IIS version when running the installer. HttpRuntime.IISVersion will return `null` if the application running is not hosted by IIS, so this answer doesn't appear to help the OP. – Beofett Mar 28 '16 at 20:50
4

Use System.Web.HttpRequest.ServerVariables("SERVER_SOFTWARE"). The return value is a string in the format name/version.

gkrogers
  • 8,126
  • 3
  • 29
  • 36
3

You can use below code

public static bool IisInstalled()
        {
            try
            {
                using (RegistryKey iisKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\InetStp"))
                {
                    return (int)iisKey.GetValue("MajorVersion") >= 6;
                }
            }
            catch
            {
                return false;
            }
        }

fore more information visit : http://www.java2s.com/Code/CSharp/Windows/IIShelperisIISInstalledIISstateIISversion.htm

Mahmut EFE
  • 5,137
  • 5
  • 46
  • 56
3

It is usually presented in http header of response, as i know.

0x49D1
  • 8,505
  • 11
  • 76
  • 127
2

For installer with custom actions: In your custom action view, you can pass data to your customer installer class via the "CustomActionData" attribute in the properties for the custom action as follows: /iisv="[IISVERSION]"

Check:

http://johnbarshinger.wordpress.com/2006/10/27/how-to-modify-the-vs2005-installer-to-set-the-asp-net-version-and-create-application-pools/

mas_oz2k1
  • 2,851
  • 3
  • 34
  • 41
0

I would just check the version of the OS: xp has IIS 5.1, Server 2003 has IIS 6 and vista/Server 2008 has IIS 7.

Here's how to check the version of the OS.

Community
  • 1
  • 1
Lodewijk
  • 2,380
  • 4
  • 25
  • 40
-2

Check the X-Powered-By header: http://www.http-stats.com/X-Powered-By

There you can find the possibly values...

Ironicnet
  • 547
  • 3
  • 14