【C#/C++】使用托管代码检测IIS是否安装并注册了ASP/ASP.NET等服务
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
介绍我之前关于使用托管代码检测安装了哪些 .NET Framework 版本和服务包的文章生成了一些与检测 Internet 信息服务 (IIS) 是否已安装以及 ASP 或 ASP.NET 是否已注册相关的后续问题。 由于这是一个托管代码解决方案,它确实需要已经安装了 .NET Framework 的一个版本,因此不能保证此类作为安装过程的一部分工作。如果您需要在安装过程中可靠地执行此操作,则需要考虑使用非托管 C++ 执行相同的工作。 背景确定是否安装了特定版本的 Internet 信息服务的正确方法是在注册表中查找以下项: HKLM\Software\Microsoft\InetStp\MajorVersion 这是一个 DWORD 值,指示安装的 IIS 版本(如果存在)。
对于 IIS 5,您可以使用 MinorVersion DWORD 值来确定您是在 Windows 2000 Server 还是 Windows XP Professional 上运行。如果 MinorVersion 为 1,则您在 Windows XP Professional 上运行。 检测 IIS 子组件有时,仅仅知道是否安装了 IIS 是不够的,您需要确定是否还安装了特定的子组件。同样,我们可以转向注册表以获取此信息。所有子组件信息都包含在以下注册表项中: HKLM\Software\Microsoft\Windows\CurrentVersion\Setup\Oc Manager\Subcomponents 此键下的所有值都是 DWORD,因此如果值为 1,则安装该组件。
检测是否已注册 ASP 或 ASP.NET为了检测 ASP 是否在 IIS 中注册,您可以简单地查看是否安装了 ASP 组件 (iis_asp)。但是,对于 ASP.NET,由于有不同版本的 ASP.NET,它变得有点复杂。我们还可以通过查看以下键来使用注册表获取此信息:
如果该键存在,则该版本的 ASP.NET 已向 IIS 注册。 使用代码为了整合检查所有各种注册表项并帮助隔离 .NET Framework 和 IIS 的未来版本的更改,
正如你所看到的,这些函数使用 C#
/// <span class="code-SummaryComment"><summary></span> /// Specifies the .NET Framework versions /// <span class="code-SummaryComment"></summary></span> public enum FrameworkVersion { /// <span class="code-SummaryComment"><summary></span> /// .NET Framework 1.0 /// <span class="code-SummaryComment"></summary></span> Fx10, /// <span class="code-SummaryComment"><summary></span> /// .NET Framework 1.1 /// <span class="code-SummaryComment"></summary></span> Fx11, /// <span class="code-SummaryComment"><summary></span> /// .NET Framework 2.0 /// <span class="code-SummaryComment"></summary></span> Fx20, /// <span class="code-SummaryComment"><summary></span> /// .NET Framework 3.0 /// <span class="code-SummaryComment"></summary></span> Fx30, /// <span class="code-SummaryComment"><summary></span> /// .NET Framework 3.5 (Orcas) /// <span class="code-SummaryComment"></summary></span> Fx35, } /// <span class="code-SummaryComment"><summary></span> /// Specifies the Internet Information Services (IIS) versions /// <span class="code-SummaryComment"></summary></span> public enum InternetInformationServicesVersion { /// <span class="code-SummaryComment"><summary></span> /// Internet Information Services 4 /// <span class="code-SummaryComment"></summary></span> /// <span class="code-SummaryComment"><remarks>Shipped in NT Option Pack for Windows NT 4</remarks></span> IIS4, /// <span class="code-SummaryComment"><summary></span> /// Internet Information Services 5 /// <span class="code-SummaryComment"></summary></span> /// <span class="code-SummaryComment"><remarks>Shipped in Windows 2000 Server and Windows XP Professional</remarks></span> IIS5, /// <span class="code-SummaryComment"><summary></span> /// Internet Information Services 6 /// <span class="code-SummaryComment"></summary></span> /// <span class="code-SummaryComment"><remarks>Shipped in Windows Server 2003</remarks></span> IIS6, /// <span class="code-SummaryComment"><summary></span> /// Internet Information Services 7 /// <span class="code-SummaryComment"></summary></span> /// <span class="code-SummaryComment"><remarks>Shipped in Windows Vista</remarks></span> IIS7, } /// <span class="code-SummaryComment"><summary></span> /// Specifies the Internet Information Services (IIS) versions /// <span class="code-SummaryComment"></summary></span> public enum InternetInformationServicesComponent { /// <span class="code-SummaryComment"><summary></span> /// Internet Information Services Common Files /// <span class="code-SummaryComment"></summary></span> Common, /// <span class="code-SummaryComment"><summary></span> /// Active Server Pages (ASP) for Internet Information Services /// <span class="code-SummaryComment"></summary></span> ASP, /// <span class="code-SummaryComment"><summary></span> /// File Transfer Protocol (FTP) service /// <span class="code-SummaryComment"></summary></span> FTP, /// <span class="code-SummaryComment"><summary></span> /// Internet Information Services Manager /// <span class="code-SummaryComment"></summary></span> InetMgr, /// <span class="code-SummaryComment"><summary></span> /// Internet Data Connector /// <span class="code-SummaryComment"></summary></span> InternetDataConnector, /// <span class="code-SummaryComment"><summary></span> /// Network News Transfer Protocol (NNTP) service /// <span class="code-SummaryComment"></summary></span> NNTP, /// <span class="code-SummaryComment"><summary></span> /// Server-Side Includes /// <span class="code-SummaryComment"></summary></span> ServerSideIncludes, /// <span class="code-SummaryComment"><summary></span> /// Simple Mail Transfer Protocol (SMTP) service /// <span class="code-SummaryComment"></summary></span> SMTP, /// <span class="code-SummaryComment"><summary></span> /// Web Distributed Authoring and Versioning (WebDAV) publishing /// <span class="code-SummaryComment"></summary></span> WebDAV, /// <span class="code-SummaryComment"><summary></span> /// World Wide Web (WWW) service /// <span class="code-SummaryComment"></summary></span> WWW, /// <span class="code-SummaryComment"><summary></span> /// Remote administration (HTML) /// <span class="code-SummaryComment"></summary></span> RemoteAdmin, /// <span class="code-SummaryComment"><summary></span> /// Internet Server Application Programming Interface (ISAPI) for /// Background Intelligent Transfer Service (BITS) server extensions /// <span class="code-SummaryComment"></summary></span> BitsISAPI, /// <span class="code-SummaryComment"><summary></span> /// Background Intelligent Transfer Service (BITS) server extensions /// <span class="code-SummaryComment"></summary></span> Bits, /// <span class="code-SummaryComment"><summary></span> /// FrontPage server extensions /// <span class="code-SummaryComment"></summary></span> FrontPageExtensions, /// <span class="code-SummaryComment"><summary></span> /// Internet printing /// <span class="code-SummaryComment"></summary></span> InternetPrinting, /// <span class="code-SummaryComment"><summary></span> /// ActiveX control and sample pages for hosting Terminal Services /// client connections over the web /// <span class="code-SummaryComment"></summary></span> TSWebClient, } C# 中的完整示例如下所示: 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)); } 这些 需要注意的是,如果用户没有访问注册表的适当权限,该函数将抛出一个异常,该异常将向上冒泡给原始调用者。这样做是为了让调用者能够根据抛出的异常采取不同的操作。 附件:MSNET20.zip 该文章在 2021/6/11 17:48:18 编辑过 |
关键字查询
相关文章
正在查询... |