What would the preferred way of programmatically determining which the currently installed version of Microsoft Internet Information Services (IIS) is?
I know that i
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
}
}
public int GetIISVersion()
{
RegistryKey parameters = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\W3SVC\\Parameters");
int MajorVersion = (int)parameters.GetValue("MajorVersion");
return MajorVersion;
}
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
}
}
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.
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
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.