« Previous Next »

Thread: How to know which IIS versions are installed from a WinForm in C#?

Last post 11-09-2009 4:10 AM by ErTelis. 5 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (6 items)

Sort Posts:

  • 11-06-2009, 10:19 AM

    • ErTelis
    • Not Ranked
    • Joined on 11-05-2009, 12:06 PM
    • Posts 6

    How to know which IIS versions are installed from a WinForm in C#?

    Hello,

     I was looking all over the Internet to find an answer I'm sure there must be an easy way to get the IIS versions running on the Server.

    My old methods aren't working anymore on Windows 7 and Windows Server 2008:
    Old broken Method:

    DirectoryEntry entry = null; eWebServerVersions iisVersion = eWebServerVersions.Unknown;

    try

    {

    entry =
    new DirectoryEntry("IIS://LOCALHOST/W3SVC/INFO");

    }

    catch

    {

    trace.Log(
    EnumTraceLevel.methods, "IISAppPool.cs", "GetIISServerVersion", "UNKNOWN VERSION");

    }

    int ver = 5;

    try

    {

    ver = (
    int)entry.Properties["MajorIISVersionNumber"].Value;

    }

     

    I hope there is a nice clean way multiplatform ...

    Something like

    ServerManager iisManager = new ServerManager();

    iisManager.Version !!!!!


    Or do I have to check which platform I'm on and then assume the IIS version from it?

    Like it says here: http://stackoverflow.com/questions/446390/how-to-detect-iis-version-using-c

    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.

     Thanks a lot for your help

  • 11-06-2009, 1:20 PM In reply to

    • ErTelis
    • Not Ranked
    • Joined on 11-05-2009, 12:06 PM
    • Posts 6

    Re: How to know which IIS versions are installed from a WinForm in C#?

    The most elegant way to retrieve the IIS version on XP as well as Windows 7 so far is:

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

    string version =  verinfo.FileMajorPart;

  • 11-06-2009, 1:36 PM In reply to

    • ksingla
    • Top 25 Contributor
    • Joined on 06-14-2006, 3:02 AM
    • Redmond, WA
    • Posts 863

    Re: How to know which IIS versions are installed from a WinForm in C#?

    IIS major and minor version is available under the following registry key as well. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp

    Thanks,
    Kanwal

    Follow me on twitter at http://twitter.com/kjsingla
  • 11-06-2009, 1:49 PM In reply to

    • ErTelis
    • Not Ranked
    • Joined on 11-05-2009, 12:06 PM
    • Posts 6

    Re: How to know which IIS versions are installed from a WinForm in C#?

    I found reading in the registry a little bit cumbersome, would you have the C# code to it quickly?

    Thx ;)

  • 11-06-2009, 2:05 PM In reply to

    Re: How to know which IIS versions are installed from a WinForm in C#?

    Here is a quick way you can get a Version out of it:

    Note that I typed it in notepad so it could have errors, but the idea should work :)

    Version GetIisVersion() {
      using (RegistryKey componentsKey =
        Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\INETSTP\Components", 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);
    }

  • 11-09-2009, 4:10 AM In reply to

    • ErTelis
    • Not Ranked
    • Joined on 11-05-2009, 12:06 PM
    • Posts 6

    Re: How to know which IIS versions are installed from a WinForm in C#?

    Great! thanks for that, my previous solution wasn't working on Windows 2008 so I'll use the registry method instead.

    CarlosAg>I changed the registry path to where I can read the IIS version to this:

    public Version GetIisVersionMS()
    {
    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);
    }
    }

    Tested on Windows 7, Windows 2008 and XP :)

Page 1 of 1 (6 items)
Microsoft Communities