9

What would the preferred way of programmatically determining which the currently installed version of Microsoft Internet Information Services (IIS) is?

I know that it can be found by looking at the MajorVersion key in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters.

Would this be the recommended way of doing it, or is there any safer or more beautiful method available to a .NET developer?

Cœur
  • 37,241
  • 25
  • 195
  • 267
SteinNorheim
  • 2,197
  • 1
  • 15
  • 21

6 Answers6

5
public int GetIISVersion()
{
     RegistryKey parameters = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\W3SVC\\Parameters");
     int MajorVersion = (int)parameters.GetValue("MajorVersion");

     return MajorVersion;
}
  • Is this the recommended way of doing it? Ie, does it work in all existing releases, and is it likely to still work in future versions? – SteinNorheim Feb 24 '11 at 07:56
  • What's about this key: HKLM:\SOFTWARE\Microsoft\InetStp\ and setupstring,versionstring values ? – Kiquenet Jan 23 '14 at 14:25
  • @SteinNorheim A lot of the installers use this key, and WiX IIS extension sticks to this. All supported IIS releases today (IIS 7-10) properly generate this key. I see no reason that Microsoft to break this in the near future. – Lex Li Nov 20 '18 at 15:23
4

To identify the version from outside the IIS process, one possibility is like below...

string w3wpPath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.System), 
    @"inetsrv\w3wp.exe");
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(w3wpPath);
Console.WriteLine(versionInfo.FileMajorPart);

To identify it from within the worker process at runtime...

using (Process process = Process.GetCurrentProcess())
{
    using (ProcessModule mainModule = process.MainModule)
    {
        // main module would be w3wp
        int version = mainModule.FileVersionInfo.FileMajorPart
    }
}
Shiva
  • 18,235
  • 4
  • 20
  • 9
1

You could build a WebRequest and send it to port 80 on a loopback IP address and get the Server HTTP header.

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://127.0.0.1/");
HttpWebResponse myHttpWebResponse = null;
try
{
    myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
}
catch (WebException ex)
{
    myHttpWebResponse = (HttpWebResponse)ex.Response;
}
string WebServer = myHttpWebResponse.Headers["Server"];
myHttpWebResponse.Close();

Not sure if that's a better way of doing it but it's certainly another option.

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
0

I did it this way (using Powershell):

function Validate-IISVersion([switch] $ContinueOnError = $false)
{
if ($ContinueOnError)
{ $ErrorActionPreference = "SilentlyContinue" }
else
{ $ErrorActionPreference = "Stop" }

# Using GAC to ensure the IIS (assembly) version
$IISAssembly = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$IISVersion = $IISAssembly.GetName().Version
$IISVersionString = [string]::Format("{0}.{1}.{2}.{3}", $IISVersion.Major, $IISVersion.Minor, $IISVersion.Build, $IISVersion.Revision)
if (!$IISVersionString.Equals("7.0.0.0"))
{
    if ($ContinueOnError)
    {
        Write-Host  "`nConflicting IIS version found! [Version: $IISVersionString]`t    " -NoNewline -ForegroundColor Red
    }
    Write-Error "Conflicting IIS version found [$IISVersionString]! @ $(Split-Path $MyInvocation.ScriptName -leaf)"
    return $false
}
else
{
    return $true
}
}
gnat
  • 6,213
  • 108
  • 53
  • 73
Vaibhav
  • 2,527
  • 1
  • 27
  • 31
  • Can you difference IIS 7.0 and IIS 7.5 using Microsoft.Web.Administration? better use Windows Registry, I think, IMHO – Kiquenet Jan 23 '14 at 14:26
  • " Built on the .NET Framework, Windows PowerShell helps IT professionals and power users control and automate the administration of the Windows operating system and applications that run on Windows." https://technet.microsoft.com/en-us/library/bb978526.aspx – Vaibhav Apr 03 '15 at 04:31
  • @Vaibhav so? And .NET is written with C or C++. – Shadow The GPT Wizard Apr 03 '15 at 06:14
  • I don't get your point @ShadowWizard. Why do you regard the above code written in POwershell using .net assembly (GAC) as 'not .NET'? – Vaibhav Apr 03 '15 at 06:29
  • 1
    The OP is .NET developer, looking for .NET way of doing it. He did not mention PowerShell, or Java, or Assembly, or Python, etc. – Shadow The GPT Wizard Apr 03 '15 at 07:31
0

No need to write code. You can find it in Registry editor

goto to run -> type - regedit ->

The LOCAL MACHINE Branch of registry contains the Version information for Windows 7.

The Starting Branch is in (HKLM) HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \InetStp\ VersionString

Note: The Spaces are for reading purposes.

Rohan Khude
  • 4,455
  • 5
  • 49
  • 47
0

The below command helped me find the IIS version correctly on IIS 8.5 (Windows 2012 R2) and 7.5 Windows 7 SP1.

[System.Diagnostics.FileVersionInfo]::GetVersionInfo("$env:SystemRoot\system32\inetsrv\InetMgr.exe").ProductVersion

Reference:

https://forums.iis.net/p/1171695/1984536.aspx : Answer from f00_beard