Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically determine installed IIS version

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?

like image 837
SteinNorheim Avatar asked Jan 12 '09 10:01

SteinNorheim


People also ask

How do I tell what version of IIS is installed?

Double click on the Internet Information Services (IIS) Manager to open it. Click Help from the menu bar. Choose About Internet Information Services from the drop-down list. The version information will be displayed in the pop-up window.

How do I know if IIS is installed on a remote server?

Start by hitting the WINKEY + R button combination to launch the Run utility, type in '%SystemRoot%\system32\inetsrv\InetMgr.exe' and hit Enter. Also, you can enter inetmgr and hit Enter to launch the same IIS Manager and follow the same steps as for the Command Prompt method.


3 Answers

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

     return MajorVersion;
}
like image 168
Dušan Stanojević Avatar answered Sep 19 '22 08:09

Dušan Stanojević


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
    }
}
like image 42
Shiva Avatar answered Sep 22 '22 08:09

Shiva


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.

like image 37
Spencer Ruport Avatar answered Sep 18 '22 08:09

Spencer Ruport