使用托管代码检测 IIS 是否已安装以及 ASP/ASP.NET 是否已注册






4.60/5 (15投票s)
解释了如何使用托管代码检测已安装的 Internet Information Services (IIS) 的版本以及 ASP 或 ASP.NET 是否已注册。
引言
我早些时候关于使用托管代码检测已安装的 .NET Framework 版本和 Service Pack 的 文章 [^] 产生了一些后续问题,这些问题与检测 Internet Information Services (IIS) 是否已安装以及 ASP 或 ASP.NET 是否已注册有关。
由于这是一个托管代码解决方案,它确实需要已安装 .NET Framework 的某个版本,因此此类不能保证在安装过程中起作用。如果您需要在安装过程中可靠地执行此操作,则需要使用非托管 C++ 来完成相同的工作。
背景
确定 Internet Information Services 的特定版本是否已安装的正确方法是查找注册表中以下项:
HKLM\Software\Microsoft\InetStp\MajorVersion
如果存在,这是一个 DWORD 值,表示已安装的 IIS 版本。
MajorVersion | IIS 版本 | 描述 |
4 | IIS 4 | 随 Windows NT 4 的 NT Option Pack 发布 |
5 | IIS 5 | 随 Windows 2000 Server 和 Windows XP Professional 发布 |
6 | IIS 6 | 随 Windows Server 2003 发布 |
7 | IIS 7 | 随 Windows Vista 发布 |
对于 IIS 5,您可以使用 MinorVersion DWORD 值来确定您是在 Windows 2000 Server 还是 Windows XP Professional 上运行。如果 MinorVersion 为 1,则表示您在 Windows XP Professional 上运行。
检测 IIS 子组件
有时,仅仅知道 IIS 是否已安装是不够的,您还需要确定是否已安装特定的子组件。同样,我们可以通过注册表获取此信息。所有子组件信息都包含在以下注册表中:
HKLM\Software\Microsoft\Microsoft\Windows\CurrentVersion\Setup\Oc Manager\Subcomponents
此项下的所有值都是 DWORD,因此如果值为 1,则表示该组件已安装。
IIS 子组件 | 注册表值 |
IIS 公共文件 | iis_common |
IIS 的 Active Server Pages (ASP) | iis_asp |
文件传输协议 (FTP) 服务 | iis_ftp |
IIS 管理器 (Microsoft Management Console [MMC] 管理单元) | iis_inetmgr |
Internet 数据连接器 | iis_internetdataconnector |
网络新闻传输协议 (NNTP) 服务 | iis_nntp |
服务器端包含 | iis_serversideincludes |
简单邮件传输协议 (SMTP) 服务 | iis_smtp |
Web 分布式创作和版本控制 (WebDAV) 发布 | iis_webdav |
万维网 (WWW) 服务 | iis_www |
远程管理 (HTML) | sakit_web |
Internet 服务器应用程序编程接口 (ISAPI) 用于后台智能传输服务 (BITS) 服务器扩展 |
BitsServerExtensionsISAPI |
后台智能传输服务 (BITS) 服务器扩展管理单元 |
BitsServerExtensionsManager |
FrontPage 服务器扩展 | fp_extensions |
Internet 打印 | inetprint |
用于托管的 ActiveX 控件和示例页面 通过 Web 的终端服务客户端连接 |
TSWebClient |
检测 ASP 或 ASP.NET 是否已注册
为了检测 ASP 是否与 IIS 注册,您只需查看 ASP 组件 (iis_asp) 是否已安装即可。但是,对于 ASP.NET,情况会更复杂一些,因为有不同版本的 ASP.NET。我们也可以通过注册表获取此信息,查看以下项:
框架版本 | 注册表项 |
ASP.NET v1.1 | HKLM\Software\Microsoft\ASP.NET\1.1.4322.0 |
ASP.NET v2.0 | HKLM\Software\Microsoft\ASP.NET\2.0.50727.0 |
如果该项存在,则表示该版本的 ASP.NET 已与 IIS 注册。
Using the Code
为了统一检查所有各种注册表项并帮助隔离未来版本的 .NET Framework 和 IIS 的更改,创建了 `InternetInformationServicesDetection` 类。此类公开了以下 `public` 方法:
-
public static bool IsInstalled(InternetInformationServicesVersion iisVersion)
-
public static bool IsInstalled(InternetInformationServicesComponent subcomponent)
-
public static bool IsAspRegistered()
-
public static bool IsAspNetRegistered(FrameworkVersion frameworkVersion)
如您所见,这些函数使用了 `FrameworkVersion`、`InternetInformationServicesVersion` 和 `InternetInformationServicesComponent` 枚举。这些枚举具有以下定义:
/// <summary>
/// Specifies the .NET Framework versions
/// </summary>
public enum FrameworkVersion
{
/// <summary>
/// .NET Framework 1.0
/// </summary>
Fx10,
/// <summary>
/// .NET Framework 1.1
/// </summary>
Fx11,
/// <summary>
/// .NET Framework 2.0
/// </summary>
Fx20,
/// <summary>
/// .NET Framework 3.0
/// </summary>
Fx30,
/// <summary>
/// .NET Framework 3.5 (Orcas)
/// </summary>
Fx35,
}
/// <summary>
/// Specifies the Internet Information Services (IIS) versions
/// </summary>
public enum InternetInformationServicesVersion
{
/// <summary>
/// Internet Information Services 4
/// </summary>
/// <remarks>Shipped in NT Option Pack for Windows NT 4</remarks>
IIS4,
/// <summary>
/// Internet Information Services 5
/// </summary>
/// <remarks>Shipped in Windows 2000 Server and Windows XP Professional</remarks>
IIS5,
/// <summary>
/// Internet Information Services 6
/// </summary>
/// <remarks>Shipped in Windows Server 2003</remarks>
IIS6,
/// <summary>
/// Internet Information Services 7
/// </summary>
/// <remarks>Shipped in Windows Vista</remarks>
IIS7,
}
/// <summary>
/// Specifies the Internet Information Services (IIS) versions
/// </summary>
public enum InternetInformationServicesComponent
{
/// <summary>
/// Internet Information Services Common Files
/// </summary>
Common,
/// <summary>
/// Active Server Pages (ASP) for Internet Information Services
/// </summary>
ASP,
/// <summary>
/// File Transfer Protocol (FTP) service
/// </summary>
FTP,
/// <summary>
/// Internet Information Services Manager
/// </summary>
InetMgr,
/// <summary>
/// Internet Data Connector
/// </summary>
InternetDataConnector,
/// <summary>
/// Network News Transfer Protocol (NNTP) service
/// </summary>
NNTP,
/// <summary>
/// Server-Side Includes
/// </summary>
ServerSideIncludes,
/// <summary>
/// Simple Mail Transfer Protocol (SMTP) service
/// </summary>
SMTP,
/// <summary>
/// Web Distributed Authoring and Versioning (WebDAV) publishing
/// </summary>
WebDAV,
/// <summary>
/// World Wide Web (WWW) service
/// </summary>
WWW,
/// <summary>
/// Remote administration (HTML)
/// </summary>
RemoteAdmin,
/// <summary>
/// Internet Server Application Programming Interface (ISAPI) for
/// Background Intelligent Transfer Service (BITS) server extensions
/// </summary>
BitsISAPI,
/// <summary>
/// Background Intelligent Transfer Service (BITS) server extensions
/// </summary>
Bits,
/// <summary>
/// FrontPage server extensions
/// </summary>
FrontPageExtensions,
/// <summary>
/// Internet printing
/// </summary>
InternetPrinting,
/// <summary>
/// ActiveX control and sample pages for hosting Terminal Services
/// client connections over the web
/// </summary>
TSWebClient,
}
完整的 C# 示例如下所示:
bool iis4Installed =
InternetInformationServicesDetection.IsInstalled
(InternetInformationServicesVersion.IIS4);
bool iis5Installed =
InternetInformationServicesDetection.IsInstalled
(InternetInformationServicesVersion.IIS5);
bool iis6Installed =
InternetInformationServicesDetection.IsInstalled
(InternetInformationServicesVersion.IIS6);
bool iis7Installed =
InternetInformationServicesDetection.IsInstalled
(InternetInformationServicesVersion.IIS7);
Console.WriteLine("IIS 4 installed? {0}", iis4Installed);
Console.WriteLine("IIS 5 installed? {0}", iis5Installed);
Console.WriteLine("IIS 6 installed? {0}", iis6Installed);
Console.WriteLine("IIS 7 installed? {0}", iis7Installed);
if (iis4Installed || iis5Installed || iis6Installed || iis7Installed)
{
Console.WriteLine("ASP Registered? {0}",
InternetInformationServicesDetection.IsAspRegistered());
Console.WriteLine("ASP.NET 1.0 Registered? {0}",
InternetInformationServicesDetection.IsAspNetRegistered(FrameworkVersion.Fx10));
Console.WriteLine("ASP.NET 1.1 Registered? {0}",
InternetInformationServicesDetection.IsAspNetRegistered(FrameworkVersion.Fx11));
Console.WriteLine("ASP.NET 2.0 Registered? {0}",
InternetInformationServicesDetection.IsAspNetRegistered(FrameworkVersion.Fx20));
// These really don't exist, they are actually the .NET 2.0 version of ASP.NET.
Console.WriteLine("ASP.NET 3.0 Registered? {0}",
InternetInformationServicesDetection.IsAspNetRegistered(FrameworkVersion.Fx30));
Console.WriteLine("ASP.NET 3.5 Registered? {0}",
InternetInformationServicesDetection.IsAspNetRegistered(FrameworkVersion.Fx35));
}
关注点
`public` 方法只是包装器,用于确定应调用哪个 `private` 函数。这些 `private` 函数反过来查询相应的注册表项并处理结果。但是,实际工作由 `GetRegistryValue
需要注意的是,如果用户没有适当的权限访问注册表,此函数将抛出一个异常,该异常会冒泡到原始调用者。这样做是为了允许调用者能够根据抛出的异常采取不同的操作。
未来考虑
Windows XP 64 位和 Windows Vista 64 位支持。如果有人想在这些操作系统上测试此功能,并告诉我它是否运行正常,如果不正常,错误是什么,我将进行修正。
历史
- 2007 年 4 月 6 日
- 原文
- 2007 年 8 月 17 日
- 更新了下载,包括对 .NET Framework v3.5 Beta 2 的 `FrameworkVersionDetection` 类进行的修复。