如何以编程方式确定已安装的 IIS 版本

发布于 2024-07-10 20:18:01 字数 237 浏览 9 评论 0原文

以编程方式确定当前安装的 Microsoft Internet 信息服务 (IIS) 版本的首选方法是什么?

我知道可以通过查看 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters 中的 MajorVersion 键找到它。

这是否是推荐的实现方式,或者是否有任何更安全或更美观的方法可供 .NET 开发人员使用?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

青衫负雪 2024-07-17 20:18:01
public int GetIISVersion()
{
     RegistryKey parameters = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\W3SVC\\Parameters");
     int MajorVersion = (int)parameters.GetValue("MajorVersion");

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

     return MajorVersion;
}
-小熊_ 2024-07-17 20:18:01

要从 IIS 进程外部识别版本,一种可能性如下...

string w3wpPath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.System), 
    @"inetsrv\w3wp.exe");
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(w3wpPath);
Console.WriteLine(versionInfo.FileMajorPart);

要在运行时从工作进程内部识别它...

using (Process process = Process.GetCurrentProcess())
{
    using (ProcessModule mainModule = process.MainModule)
    {
        // main module would be w3wp
        int version = mainModule.FileVersionInfo.FileMajorPart
    }
}

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
    }
}
許願樹丅啲祈禱 2024-07-17 20:18:01

您可以构建一个 WebRequest 并将其发送到环回 IP 地址上的端口 80,并获取服务器 HTTP 标头。

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();

不确定这是否是更好的方法,但这肯定是另一种选择。

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.

·深蓝 2024-07-17 20:18:01

我是这样做的(使用Powershell):

function Validate-IISVersion([switch] $ContinueOnError = $false)
{
if ($ContinueOnError)
{ $ErrorActionPreference = "SilentlyContinue" }
else
{ $ErrorActionPreference = "Stop" }

# Using GAC to ensure the IIS (assembly) version
$IISAssembly = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$IISVersion = $IISAssembly.GetName().Version
$IISVersionString = [string]::Format("{0}.{1}.{2}.{3}", $IISVersion.Major, $IISVersion.Minor, $IISVersion.Build, $IISVersion.Revision)
if (!$IISVersionString.Equals("7.0.0.0"))
{
    if ($ContinueOnError)
    {
        Write-Host  "`nConflicting IIS version found! [Version: $IISVersionString]`t    " -NoNewline -ForegroundColor Red
    }
    Write-Error "Conflicting IIS version found [$IISVersionString]! @ $(Split-Path $MyInvocation.ScriptName -leaf)"
    return $false
}
else
{
    return $true
}
}

I did it this way (using Powershell):

function Validate-IISVersion([switch] $ContinueOnError = $false)
{
if ($ContinueOnError)
{ $ErrorActionPreference = "SilentlyContinue" }
else
{ $ErrorActionPreference = "Stop" }

# Using GAC to ensure the IIS (assembly) version
$IISAssembly = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$IISVersion = $IISAssembly.GetName().Version
$IISVersionString = [string]::Format("{0}.{1}.{2}.{3}", $IISVersion.Major, $IISVersion.Minor, $IISVersion.Build, $IISVersion.Revision)
if (!$IISVersionString.Equals("7.0.0.0"))
{
    if ($ContinueOnError)
    {
        Write-Host  "`nConflicting IIS version found! [Version: $IISVersionString]`t    " -NoNewline -ForegroundColor Red
    }
    Write-Error "Conflicting IIS version found [$IISVersionString]! @ $(Split-Path $MyInvocation.ScriptName -leaf)"
    return $false
}
else
{
    return $true
}
}
变身佩奇 2024-07-17 20:18:01

无需编写代码。 您可以在注册表编辑器中找到它

,转到运行-> 类型 - regedit ->

注册表的 LOCAL MACHINE 分支包含 Windows 7 的版本信息。

起始分支位于 (HKLM) HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \InetStp\ VersionString

注意:空格用于读取目的。

No need to write code. You can find it in Registry editor

goto to run -> type - regedit ->

The LOCAL MACHINE Branch of registry contains the Version information for Windows 7.

The Starting Branch is in (HKLM) HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \InetStp\ VersionString

Note: The Spaces are for reading purposes.

紫竹語嫣☆ 2024-07-17 20:18:01

以下命令帮助我在 IIS 8.5 (Windows 2012 R2) 和 7.5 Windows 7 SP1 上正确找到 IIS 版本。

[System.Diagnostics.FileVersionInfo]::GetVersionInfo("$env:SystemRoot\system32\inetsrv\InetMgr.exe").ProductVersion

参考:

https://forums.iis.net/p/1171695/1984536.aspx : f00_beard 的回答

The below command helped me find the IIS version correctly on IIS 8.5 (Windows 2012 R2) and 7.5 Windows 7 SP1.

[System.Diagnostics.FileVersionInfo]::GetVersionInfo("$env:SystemRoot\system32\inetsrv\InetMgr.exe").ProductVersion

Reference:

https://forums.iis.net/p/1171695/1984536.aspx : Answer from f00_beard

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文