【问题标题】:How to detect IIS version using C#?如何使用 C# 检测 IIS 版本?
【发布时间】:2009-01-15 11:12:32
【问题描述】:

如何使用 C# 检测 IIS 版本?

更新: 我的意思是来自一个 winapp(实际上这个场景是开发一个自定义安装程序,它想要检查已安装 IIS 的版本以调用适当的 api)

【问题讨论】:

  • 服务器端还是客户端(例如,从 winforms 应用程序调用 IIS Web)?我猜你的意思是服务器端。
  • 我的意思是来自一个winapp(实际上这个场景是开发一个自定义安装程序,想要检查安装的IIS的版本来调用适当的api)应该把它写在主要问题中..(对不起) ..

标签: c# iis


【解决方案1】:

在这里找到答案: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 上都能完美运行

【讨论】:

  • 也适用于 Windows 2003 Server!
【解决方案2】:

您可以从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 看看如何。

【讨论】:

    【解决方案3】:

    我就是这样做的。

    FileVersionInfo verinfo = FileVersionInfo.GetVersionInfo(System.Environment.SystemDirectory + @"\inetsrv\inetinfo.exe");
    
    //Tip... look at verinfo.MajorVersion.
    

    【讨论】:

    • 这会在 Windows2008R2 上引发 FileNotFoundException(即使文件存在)。看起来像是文件权限问题。
    • 64 位服务器上的 32 位进程找不到该文件,因为 SysWOW64 目录中不存在 inetsrc\inetinfo.exe。 jlafay 的注册表方法适用于所有情况,因为注册表值存在于 HKLM 的 32 位和 64 位部分中。
    【解决方案4】:

    你可以在注册表中找到它。

    您可以在此处找到 IIS 版本 6:

    HKLM\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters

    从这里的第 7 版开始:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp

    主要版本 次要版本

    【讨论】:

    • IIS 7 在 HKLM\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters 中也有一个条目,至少在 Windows7 32 位上是这样。 (主要版本 7,次要版本 5)。但这个密钥似乎不再在 Windows Server 2008 (IIS7) 上可用。
    【解决方案5】:

    使用 System.Web.HttpRequest.ServerVariables("SERVER_SOFTWARE")。返回值为名称/版本格式的字符串。

    【讨论】:

      【解决方案6】:

      在 .NET 4.5 中

      HttpRuntime.IISVersion
      

      【讨论】:

      • 不言自明...如何使用 C# 检测 IIS 版本?使用 httpRuntime.IISVersion。
      • 据我了解,OP 在运行安装程序时正在寻找 IIS 版本。如果运行的应用程序不是由 IIS 托管,HttpRuntime.IISVersion 将返回 null,所以这个答案似乎对 OP 没有帮助。
      【解决方案7】:

      据我所知,它通常出现在响应的 http 标头中。

      【讨论】:

        【解决方案8】:

        你可以使用下面的代码

        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

        【讨论】:

          【解决方案9】:

          对于具有自定义操作的安装程序: 在您的自定义操作视图中,您可以通过自定义操作属性中的“CustomActionData”属性将数据传递给您的客户安装程序类,如下所示: /iisv="[IISVERSION]"

          检查:

          http://johnbarshinger.wordpress.com/2006/10/27/how-to-modify-the-vs2005-installer-to-set-the-asp-net-version-and-create-application-pools/

          【讨论】:

            【解决方案10】:

            我只是检查操作系统的版本:xp 有 IIS 5.1,Server 2003 有 IIS 6,vista/Server 2008 有 IIS 7。

            Here's how to check the version of the OS.

            【讨论】:

              【解决方案11】:

              检查 X-Powered-By 标头: http://www.http-stats.com/X-Powered-By

              在那里你可以找到可能的值...

              【讨论】:

                猜你喜欢
                • 2013-03-28
                • 1970-01-01
                • 1970-01-01
                • 2017-10-13
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多