How to programmatically determine installed IIS version

旧巷老猫 提交于 2019-12-01 14:39:53

问题


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?


回答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.




回答2:


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

     return MajorVersion;
}



回答3:


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
    }
}



回答4:


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
}
}



回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/435050/how-to-programmatically-determine-installed-iis-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!