How to programmatically determine installed IIS version
Asked Answered
U

6

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?

Utica answered 12/1, 2009 at 10:38 Comment(0)
W
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.

Wane answered 12/1, 2009 at 10:44 Comment(2)
What's about performance using WebRequest or using Windows Registry?Fecal
This of course assumes that the default website is running on that machine, which isn't necessarily the case...Riendeau
S
5
public int GetIISVersion()
{
     RegistryKey parameters = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\W3SVC\\Parameters");
     int MajorVersion = (int)parameters.GetValue("MajorVersion");

     return MajorVersion;
}
Saliva answered 23/2, 2011 at 15:20 Comment(3)
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?Utica
What's about this key: HKLM:\SOFTWARE\Microsoft\InetStp\ and setupstring,versionstring values ?Fecal
@Utica 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.Roan
N
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
    }
}
Nicolanicolai answered 13/1, 2009 at 7:52 Comment(0)
W
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.

Wane answered 12/1, 2009 at 10:44 Comment(2)
What's about performance using WebRequest or using Windows Registry?Fecal
This of course assumes that the default website is running on that machine, which isn't necessarily the case...Riendeau
T
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
}
}
Triton answered 4/9, 2012 at 14:26 Comment(5)
Can you difference IIS 7.0 and IIS 7.5 using Microsoft.Web.Administration? better use Windows Registry, I think, IMHOFecal
" 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." technet.microsoft.com/en-us/library/bb978526.aspxTriton
@Triton so? And .NET is written with C or C++.Sitter
I don't get your point @ShadowWizard. Why do you regard the above code written in POwershell using .net assembly (GAC) as 'not .NET'?Triton
The OP is .NET developer, looking for .NET way of doing it. He did not mention PowerShell, or Java, or Assembly, or Python, etc.Sitter
S
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.

Sybille answered 19/1, 2016 at 19:45 Comment(0)
O
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

Overcheck answered 4/10, 2016 at 9:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.