如何使用C#检测IIS版本?
更新: 我的意思是从一个winapp(实际上这个场景是开发一个自定义安装程序,想要检查已安装的IIS的版本以调用相应的api)
答案 0 :(得分:35)
在这里找到答案:link text fileVersion方法在Windows 2008上不起作用,inetserv exe在我猜的其他地方。
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);
}
}
我测试了它,它在Windows XP,7和2008上完美运行
答案 1 :(得分:30)
您可以从SERVER_SOFTWARE
变量中获取此信息。它将返回以下内容:
Microsoft-IIS/5.0 (Windows 2000)
Microsoft-IIS/5.1 (Windows XP)
Microsoft-IIS/6.0 (Windows 2003 Server)
等
如果您使用的是ASP.NET,则可以通过
获取此字符串Request.ServerVariables["SERVER_SOFTWARE"];
编辑:您似乎必须查询注册表才能获取此信息。看看this page,看看如何。
答案 2 :(得分:12)
我就是这样做的。
FileVersionInfo verinfo = FileVersionInfo.GetVersionInfo(System.Environment.SystemDirectory + @"\inetsrv\inetinfo.exe");
//Tip... look at verinfo.MajorVersion.
答案 3 :(得分:6)
你可以在注册表中找到它。
在IIS版本6中,您可以在此处找到它:
HKLM \系统\ CurrentControlSet \服务\ W3SVC \参数
从版本7开始:
HKEY_LOCAL_MACHINE \ SOFTWARE \微软\ InetStp
MajorVersion MinorVersion
答案 4 :(得分:4)
使用System.Web.HttpRequest.ServerVariables(“SERVER_SOFTWARE”)。返回值是格式为name / version的字符串。
答案 5 :(得分:3)
我知道,它通常在响应的http标题中显示。
答案 6 :(得分:3)
您可以使用以下代码
public static bool IisInstalled()
{
try
{
using (RegistryKey iisKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\InetStp"))
{
return (int)iisKey.GetValue("MajorVersion") >= 6;
}
}
catch
{
return false;
}
}
更多信息请访问:http://www.java2s.com/Code/CSharp/Windows/IIShelperisIISInstalledIISstateIISversion.htm
答案 7 :(得分:2)
对于具有自定义操作的安装程序: 在自定义操作视图中,您可以通过自定义操作的属性中的“CustomActionData”属性将数据传递到客户安装程序类,如下所示: / iisv = “[IISVERSION]”
检查:
答案 8 :(得分:2)
在.NET 4.5中
HttpRuntime.IISVersion
答案 9 :(得分:0)
我只想查看操作系统的版本:xp有IIS 5.1,Server 2003有IIS 6,vista / Server 2008有IIS 7.
答案 10 :(得分:-2)
检查X-Powered-By标头: http://www.http-stats.com/X-Powered-By
在那里你可以找到可能的值......