How to detect IIS version using C#?
Update: I meant from a winapp (actually the scenario is developing a custom installer that wants to check the version of the installed IIS to call the appropriate api's)
Found the answer here: link text The fileVersion method dosesn't work on Windows 2008, the inetserv exe is somewhere else I guess.
public Version GetIisVersion()
{
using (RegistryKey componentsKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\InetStp", false))
{
if (componentsKey != null)
{
int majorVersion = (int)componentsKey.GetValue("MajorVersion", -1);
int minorVersion = (int)componentsKey.GetValue("MinorVersion", -1);
if (majorVersion != -1 && minorVersion != -1)
{
return new Version(majorVersion, minorVersion);
}
}
return new Version(0, 0);
}
}
I tested it, it works perfectly on Windows XP, 7 and 2008
You can get this information from the SERVER_SOFTWARE
variable. It will return the following:
Microsoft-IIS/5.0 (Windows 2000)
Microsoft-IIS/5.1 (Windows XP)
Microsoft-IIS/6.0 (Windows 2003 Server)
etc.
If you're using ASP.NET, you can get this string via
Request.ServerVariables["SERVER_SOFTWARE"];
EDIT: It seems that you will have to query the registry to get this information. Take a look at this page to see how.
This is how i do it.
FileVersionInfo verinfo = FileVersionInfo.GetVersionInfo(System.Environment.SystemDirectory + @"\inetsrv\inetinfo.exe");
//Tip... look at verinfo.MajorVersion.
U can find it in the registry.
Up to IIS version 6 you can find it here:
HKLM\\SYSTEM\\CurrentControlSet\\Services\\W3SVC\\Parameters
Since version 7 here:
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\InetStp
MajorVersion MinorVersion
Use System.Web.HttpRequest.ServerVariables("SERVER_SOFTWARE"). The return value is a string in the format name/version.
You can use below code
public static bool IisInstalled()
{
try
{
using (RegistryKey iisKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\InetStp"))
{
return (int)iisKey.GetValue("MajorVersion") >= 6;
}
}
catch
{
return false;
}
}
fore more information visit : http://www.java2s.com/Code/CSharp/Windows/IIShelperisIISInstalledIISstateIISversion.htm
在.NET 4.5中
HttpRuntime.IISVersion
如我所知,它通常以响应的http标头显示。
For installer with custom actions: In your custom action view, you can pass data to your customer installer class via the "CustomActionData" attribute in the properties for the custom action as follows: /iisv="[IISVERSION]"
Check:
I would just check the version of the OS: xp has IIS 5.1, Server 2003 has IIS 6 and vista/Server 2008 has IIS 7.
Check the X-Powered-By header: http://www.http-stats.com/X-Powered-By
There you can find the possibly values...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.