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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/446390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:41:07  来源:igfitidea点击:

How to detect IIS version using C#?

c#iis

提问by Amr Elsehemy

How to detect IIS version using C#?

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

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)

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

采纳答案by Igal Tabachnik

You can get this information from the SERVER_SOFTWAREvariable. It will return the following:

您可以从SERVER_SOFTWARE变量中获取此信息。它将返回以下内容:

Microsoft-IIS/5.0 (Windows 2000)
Microsoft-IIS/5.1 (Windows XP)
Microsoft-IIS/6.0 (Windows 2003 Server)

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

如果您使用的是 ASP.NET,则可以通过以下方式获取此字符串

Request.ServerVariables["SERVER_SOFTWARE"];

EDIT: It seems that you will have to query the registry to get this information. Take a look at this pageto see how.

编辑:似乎您必须查询注册表才能获取此信息。看看这个页面,看看如何。

回答by Xn0vv3r

U can find it in the registry.

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

Up to IIS version 6 you can find it here:

直到 IIS 版本 6,您都可以在此处找到它:

HKLM\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters

HKLM\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters

Since version 7 here:

从这里的第 7 版开始:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp

MajorVersion MinorVersion

主要版本 次要版本

回答by Ironicnet

Check the X-Powered-By header: http://www.http-stats.com/X-Powered-By

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

There you can find the possibly values...

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

回答by 0x49D1

It is usually presented in http header of response, as i know.

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

回答by gkrogers

Use System.Web.HttpRequest.ServerVariables("SERVER_SOFTWARE"). The return value is a string in the format name/version.

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

回答by Jesper Palm

This is how i do it.

这就是我的做法。

FileVersionInfo verinfo = FileVersionInfo.GetVersionInfo(System.Environment.SystemDirectory + @"\inetsrv\inetinfo.exe");

//Tip... look at verinfo.MajorVersion.

回答by Lodewijk

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.

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

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

以下是检查操作系统版本的方法

回答by ErTelis

Found the answer here: link textThe fileVersion method dosesn't work on Windows 2008, the inetserv exe is somewhere else I guess.

在这里找到答案:链接文本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);
    }
}

I tested it, it works perfectly on Windows XP, 7 and 2008

我测试了它,它在 Windows XP、7 和 2008 上完美运行

回答by mas_oz2k1

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]"

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

Check:

查看:

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

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

回答by Mahmut EFE

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

更多信息请访问:http: //www.java2s.com/Code/CSharp/Windows/IIShelperisIISInstalledIISstateIISversion.htm